Search code examples
androidandroid-actionbarandroid-search

Catch Click Event on UP icon for SearchView in Android.


I have implemented SearchView on my ListView. When i enter some text in EditText of this search widget, it filters the contents in my list. But if i close this search widget on clicking UP/home icon without clearing the text in EditText, i am still having the filtered list not the complete list.

So i need to catch the event of UP icon for SearchView to clear the text in EditText, or is there some other way for that.

enter image description here


Solution

  • May be, you need to try this one instead of relying on UP icon click event.

    menuItem.setOnActionExpandListener(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;
                }
            });