Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView's OnCloseListener doesn't work

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:

@Override public boolean onCreateOptionsMenu(Menu menu) {     getMenuInflater().inflate(R.menu.menu, menu);     searchView = (SearchView) menu.findItem(R.id.search_textbox).getActionView();     searchView.setOnQueryTextListener(new OnQueryTextListener() {         @Override         public boolean onQueryTextChange(String newText) {             searchLibrary(newText);             return false;         }         @Override         public boolean onQueryTextSubmit(String query) { return false; }     });     searchView.setOnCloseListener(new OnCloseListener() {         @Override         public boolean onClose() {             System.out.println("Testing. 1, 2, 3...");             return false;         }     });     return true; } 

The search works great and every is working except for the OnCloseListener. Nothing is being printed to Logcat. Here's the Logcat for when I'm pressing the "Close" button:

02-17 13:01:52.914: I/TextType(446): TextType = 0x0 02-17 13:01:57.344: I/TextType(446): TextType = 0x0 02-17 13:02:02.944: I/TextType(446): TextType = 0x0 

I've looked through the documentation and samples, but nothing seemed to change it. I'm running it on a Asus Transformer Prime and a Galaxy Nexus, both on Ice Cream Sandwich. Any ideas?

Update:

Yes - System.out.println() does work. Here's proof:

   @Override  public boolean onQueryTextChange(String newText) {     System.out.println(newText + "hello");     searchLibrary(newText);     return false;  } 

Results in this Logcat:

02-17 13:04:20.094: I/System.out(21152): hello 02-17 13:04:24.914: I/System.out(21152): thello 02-17 13:04:25.394: I/System.out(21152): tehello 02-17 13:04:25.784: I/System.out(21152): teshello 02-17 13:04:26.064: I/System.out(21152): testhello 
like image 481
Michell Bak Avatar asked Feb 17 '12 11:02

Michell Bak


2 Answers

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; } 
like image 153
niki huang Avatar answered Oct 06 '22 08:10

niki huang


For Android API 14+ (ICS and greater) use this code:

// When using the support library, the setOnActionExpandListener() method is // static and accepts the MenuItem object as an argument MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {     @Override     public boolean onMenuItemActionCollapse(MenuItem item) {         // Do something when collapsed         return true;  // Return true to collapse action view     }      @Override     public boolean onMenuItemActionExpand(MenuItem item) {         // Do something when expanded         return true;  // Return true to expand action view     } }); 

For more information: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

Ref: onActionCollapse/onActionExpand

like image 43
lomza Avatar answered Oct 06 '22 09:10

lomza