how I can set a listener into 'clear text' button in searchView (not close). And no, onQueryTextChange set to empty does not solve my problem.
Greetings
Try to get clearButton from SearchView and set OnClickListener like below:
ImageView clearButton = searchView.findViewById(androidx.appcompat.R.id.search_close_btn);
clearButton.setOnClickListener(v -> {
if(searchView.getQuery().length() == 0) {
searchView.setIconified(true);
} else {
// Do your task here
searchView.setQuery("", false);
}
});
if you got a is a TextInputLayout with id 'searchInput', then simply:
searchInput.setEndIconOnClickListener {
// Do something
}
(If you can't find it, you might have to update your libs)
Doc: https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#setEndIconOnClickListener(android.view.View.OnClickListener)
Related
In particular class, from many places call back is coming, i just want to whether it's coming text or button for example, so that I can set the data accordingly.
NOTE: I'm not talking about parent layout, I want to know exact name where the event click is happened!
If I'm doing this: Log.d("Hello", "Clicked finally: "+ view?.id)
This is coming:
D: Clicked finally: 2131296625
If you are using Kotlin, You can simply check if the View is Button or Image using is operator like:
when(view) {
is Button -> {
// a Button is clicked.
}
is AppCompatImageView -> {
// an Image is clicked.
}
else -> {
// any other view is clicked.
}
}
You can do something like this:
if (view.javaClass.simpleName == Button::class.java.simpleName) {
// it is a button
} else if (view.javaClass.simpleName == TextView::class.java.simpleName) {
// it is a text view
}
Please note that this won't work if you are using any subclass of Button or TextView. You will need to explicitly specify the class you want to check for.
I know that it is possible to perform a click on a view like this :
view.PerformClick()
How do I do it on TextInputLayout EndIcon button?
Update
The problem is that I have a bunch of InputLayouts and use a generic function to set the click listeners on them like so
fun setTextInputLayoutListeners(
inputLayout: TextInputLayout, editText: TextInputEditText,
actionSet: () -> Unit,
actionClear: () -> Unit
) {
with (inputLayout) {
setOnClickListener { actionSet() }
setEndIconOnClickListener { actionClear() }
}
editText.setOnClickListener { actionSet() }
}
and call it with different parameteres like this
setTextInputLayoutListeners(
categoryInputLayout, categoryEditText, { onCategoryClick() }, { onCategoryClear() }
)
setTextInputLayoutListeners(
dateInputLayout, dateEditText, { onDateClick() }, { onDateClear(calendar) }
)
so I'm looking for a generic solution, sort of
inputLayout.EndIcon.PerformClick()
textinput.setEndIconOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do some code
}
});
hope it helps..
EndIcon in TextInputLayout is of type CheckableImageButton and its id is R.id.text_input_end_icon (use R.id.text_input_start_icon for StartIcon). To simulate clicks, you need to find the button using findViewById, then cast it as a CheckableImageButton and call performClick().
In your case:
inputLayout.findViewById<CheckableImageButton>(R.id.text_input_end_icon)?.performClick()
Thank you so much for you clear and helpful answer.I did a little trick that worked and i wanna share my answer to everyone who will need it.
First thing i did is get the string value of my view :
Log.d("tag", String.valueOf(v));
i got that : com.google.android.material.internal.CheckableImageButton{ba604a VFED..C.. ...P.... 8,3-104,99 #7f090125 app:id/text_input_end_icon}
and like i suspected then icon is a different view of the text field layout with different id (in the end of the string value of the view).
So i changed my if condition from if (v.getId() == R.id.start_date_Layout) to if (v.getId() == R.id.text_input_end_icon), and it work now
i hope this answer will be helpful to someone, thank you again for all your answer
How to detect a click on searchView?
I have tried using setOnClickListener, setOnSearchClickListener, setOnFocusChangedListener, but to no avail.
The problem seems to be only click on textbox. If I click anywhere outside of textbox onClickListener triggers, but not if click inside the textbox.
The problem that I am trying to solve is that I need to close a if user clicks anywhere. But I dont know how to handle the SearchView.
I have managed to find a way to solve my problem.
It's a bit hackish, but it works. You have to get the editText from searchView and set OnClickListener to it
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText editText = (EditText) searchView.findViewById(id);
editText.setOnClickListener(listener);
Try this:
searchView = (SearchView)findViewById(R.id.searchView);
searchView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
searchView.setIconified(false);
}
});
Try adding app:iconifiedByDefault="false" to the SearchView tag in your layout XML.
An answer from a similar question: How to detect if SearchView is expanded?
searchView.setOnQueryTextFocusChangeListener { _ , hasFocus ->
if (hasFocus) {
// searchView expanded
} else {
// searchView not expanded
}
}
I am using searchview in my app with action bar and fragments. searchview is opened in the portrate mode and when we change the orientation it closes. If there is any text present in searchview it will not close on orientation chnage. I want to retain the state of searchview on orientation change even if the searchview is empty.How can i do this?i tried
setRetainInstance(true);
But it is not working. Please help me.
I implemented it myself since Android does not seem to save it for me.
final String s = savedSearchString;
if (s != null) {
searchViewMenuItem.expandActionView(); // opens the action view
}
searchView.post(new Runnable() {
#Override
public void run() {
searchView.setQuery(s, false); // sets the last search string on the view
}
});
'savedSearchString' is coming from the savedInstanceState. Make sure you set the initial conent of the search window on "" and not on null (default), otherwise it won't re-open an empty window.
You can create a boolean.
And when you open the searchView, you put boolean to true.
When you close the searchView, yout put boolean false.
In the onCreate, if the boolean = true, open the searchView.
final SearchView searchView = (SearchView) itemSearch.getActionView();
searchView.setQueryHint(Html
.fromHtml("<font color = #ffffff>Search...</font>"));
searchView.setOnSearchClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//your code
}
});
I want my toggle button's ON text to be large and OFF text to be small. But can't do it.. any suggestions? This is what i was trying
mf=(ToggleButton)findViewById(R.id.mf);
if(mf.isEnabled()==true)
{
mf.setTextSize(13);
}
else
mf.setTextSize(8);
Your code has to be called each time you click on your button. so use :
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (toggleButton.isChecked()) {
toggleButton.setTextSize(13.0f);
} else {
toggleButton.setTextSize(8.0f);
}
}
});
You can set OnClickListner with a easy method. In your .xml put the option
android:onClick="nameOfMethod"
And then in your code:
public void nameOfMethod(View v){
}
Where v is the togglebutton in this case ( ToggleButton mf = (ToggleButton)v; )
I put the solution here:
mf=(ToggleButton)findViewById(R.id.mf);
if(mf.isChecked())
{
mf.setTextSize(13);
}
else
mf.setTextSize(8);
Use isChecked() instead of isEnabled()
You need to do some debugging.
Firstly:
if(mf.isEnabled()==true)
can be
if (mf.isEnabled())
Does mf.setTextSize(13) on it's own work as expected?
Add some logging:
if (mf.isEnabled())
{
// Add some logging, is this line reached correctly?
mf.setTextSize(13);
}
else
// Add some logging, is this line reached correctly?
mf.setTextSize(8);
Chances are you need to change isEnabled() to isChecked(). isEnabled() means exactly that, whether it's enabled or not. You want to know whether the button has been checked.