Need to get the right searchView Listener method to place my code to change my erase typed up search result (round X) button to become a close the searchView entirely white (X) button.
Here's my code to create searchView in action bar:
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
MenuItem item =menu.findItem(menuSearch);
final SearchView searchView = (SearchView) item.getActionView();
In the 1st picture when I tap on the Search Icon, the following pops up and my code for it:
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView searchClose = searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchClose.setImageResource(R.drawable.ic_close_black_24dp);
}
});
So you can click on the (X) above and just close the searchView. Or you can start typing on the keyboard to search and you get the following: Notice now the (X) turned into a (round X) which means to delete what you're typing in keyboard.
Here's the code for above:
@Override
public boolean onQueryTextChange(String newText) {
ImageView searchCancel = searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchCancel.setImageResource(R.drawable.ic_cancel_black_24dp);
return false;
}
});
Now, if I click that (round X) again, I need it to go back to the original white (X) to be able to close the searchView Completely. So i'd need to use the first code with R.drawable.ic_close_black_24dp again.
It doesn't work on the following methods:
searchView.setOnQueryTextListener
searchView.setOnCloseListener
I know the right code, I'm just missing the right searchView Listener method. Which Listener method do I use to turn that (round X) back to white (X) when the (round X) is clicked on.
(I hope I explained it thoroughly, if not, I'd like my searchView to work like in Snapchat, you'll see it uses 2 different X's, one to delete what user typed, one to close the searchView completely).
onQueryTextChange()
is called when input is cancelled with a zero-length string. In essence, you want to show one type of "X" is the length is zero-length, otherwise, another type. Something like this:
@Override
public boolean onQueryTextChange(String newText) {
ImageView searchCancel =
searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchCancel.setImageResource(
(newText.length() > 0)
? R.drawable.ic_cancel_black_24dp:
: R.drawable.[the other type of "X"]);
return false;
}