SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(Menus.SEARCH));
searchView.setQueryHint(this.getString(R.string.search));
editSearch = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
editSearch.setHintTextColor(getResources().getColor(R.color.white));
searchView.setOnQueryTextListener(OnQuerySearchView);
private OnQueryTextListener OnQuerySearchView = new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String newText) {
if (TextUtils.isEmpty(newText)) {
listAllContact.clearTextFilter();
} else {
listAllContact.setFilterText(newText.toString());
}
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
String text = editSearch.getText().toString()
.toLowerCase(Locale.getDefault());
adapter.filter(text);
return true;
}
};
Asked
Active
Viewed 1.4k times
13

MysticMagicϡ
- 28,593
- 16
- 73
- 124

deepika taywade
- 167
- 1
- 1
- 8
-
2can you expand on your question? – sbrichards Sep 15 '14 at 07:11
-
1Here is link exactly what you http://stackoverflow.com/a/25842354/1878148 – Narendra Sep 15 '14 at 08:58
4 Answers
27
If you use android.support.v7.widget.SearchView as menu item:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:title=""
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
You can handle back button (for expanded state) with:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchItem.expandActionView();
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Write your code here
return true;
}
});
}

Atetc
- 1,643
- 19
- 26
-
-
1just put all return to TRUE or else they would not work. Worked and tested. thumbs up @Atetc – ralphgabb Mar 30 '16 at 05:56
-
correct answer. really weird for google detaching searchview onclose listener. even on 23+ API – Anfet Oct 17 '17 at 19:23
-
1This is deprecated method, use following: https://stackoverflow.com/a/48989340/4026686 – AndroidTank Mar 13 '19 at 20:09
2
MenuItemCompat.setOnActionExpandListener is now deprecated
use
menu.findItem(R.id.action_search)setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
return false;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
return false;
}
});
instead

Mohhamed Nabil
- 4,104
- 1
- 15
- 11
0
If you are using search dialog you can do something like this for Kotlin
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
return if (id == R.id.search_button) {
val searchManager = this.getSystemService(Context.SEARCH_SERVICE) as SearchManager
searchManager.setOnDismissListener {
// return the activity to the normal state
}
// set activity to search state then request search
onSearchRequested()
} else super.onOptionsItemSelected(item)
}

ykonda
- 499
- 6
- 15
-3
Here is a way of doing it -
@Override
public void onBackPressed() {
// Write your code here
super.onBackPressed();
}

Confuse
- 5,646
- 7
- 36
- 58
-
Downvote? The question isn't clearly asked and from what I understood, the asker needs to find out how to handle action when back button is pressed and my solution exactly does that. – Confuse May 14 '15 at 15:07
-
you have a point. This is the worse asked question ever. Not a single detail about the problem... – IIRed-DeathII Jun 23 '16 at 14:14
-
1This is not the answer. The question is about search view's back arrow icon press. – Chanaka Fernando Oct 26 '17 at 10:21