I\'m trying to add support for the SearchView in the Android 3.0+ ActionBar, but I can\'t get the OnCloseListener to work.
Here\'s my code:
There is no console in Android to log to. Instead, use the android logging framework:
Log.d("Test Tag", "Testing. 1, 2, 3...");
See also this question: Why doesn't "System.out.println" work in Android?
There are two common patterns for SearchView.setOnCloseListener(). This is really true of all listeners, but I'm addressing your question specifically. The first way is to create a listener function and attach it to a member variable, and the second is to make the class implement the interface and have the handler be a member function.
Creating a listener object looks like this:
private SearchView mSearchView;
private final SearchView.OnCloseListener mOnCloseListener =
new SearchView.OnCloseListener() {
public boolean onClose() {
doStuff();
return myBooleanResult;
}
};
mSearchView.setOnCloseListener(mOnCloseListener);
Implementing listener at class level looks like this:
public class MyClass implements OnCloseListener {
private SearchView mSearchView;
public MyClass(...) {
mSearchView.setOnCloseListener(this);
}
@Override
public boolean onClose() {
doStuff();
return false;
}
}
I have not seen any examples that create the OnCloseListener ad-hoc, as you did in your question.
seems an old thread already, but I thought I got the same problem API 18 in the first beginning. After googled around, found this thread, another hour read the javadoc tried and errored for something I don't pretend fully understand in javadoc, the following work for me now:
searchView.setIconifiedByDefault(true);
// OnQueryTextListener
@Override
public boolean onQueryTextSubmit(String query) {
Log.d(tag, "onQueryTextSubmit: " + query);
return true;
}
@Override
public boolean onQueryTextChange(String query) {
Log.d(tag, "onQueryTextChange: " + query);
return true;
}
// OnCloseListener
@Override
public boolean onClose() {
Log.w(tag, "onClose: ");
return false;
}
I played with true/false a bit, that somehow makes the difference, and it works for me now. Hopefully, it could save someone time.
I also meet this problem, and I have no choice but give up "oncloselistener". Instead, you can get your menuItem, then setOnActionExpandListener. Then override unimplents methods.
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// TODO Auto-generated method stub
Log.d("*******","onMenuItemActionExpand");
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//do what you want to when close the sesarchview
//remember to return true;
Log.d("*******","onMenuItemActionCollapse");
return true;
}
The reason the OnCloseListener is not called is because there is a bug in the Android code -- the listener is only called if you also call setIconifiedByDefault(true).
I encountered this issue while trying to detect the showing/dismissal of the SearchView. I ended up using a different listener and it worked for what I need:
setOnQueryTextFocusChangeListener { _, hasFocus ->
if (hasFocus) {
// SearchView is being shown
} else {
// SearchView was dismissed
}
}