I have Develop an application that have search view in action bar and i got issue when i am do search its filter perfectly but when i am press back button its still showing filter data so my question is what is event of back button of Action Bar Search view.?
My Code of Search View is
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;
}
};
Filter Method in Adapter
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
propertyList.clear();
if (charText.length() == 0) {
propertyList.addAll(arrayList);
notifyDataSetChanged();
} else {
for (ContactProperty p : arrayList) {
if (p.getFriendName().toLowerCase(Locale.getDefault())
.contains(charText)) {
propertyList.add(p);
}
}
notifyDataSetChanged();
}
Add the Search View to the App Bar To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.
SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class. For making search field visible, SearchView uses setIconifiedByDefault(false) method.
All Android devices provide a Back button for this type of navigation, so you should not add a Back button to your app's UI. Depending on the user's Android device, this button might be a physical button or a software button.
You will don't get the direct event of that searchView navigation click, there is an interface of MenuItam, the name is "MenuItem.OnActionExpandListener()". By using this interface you can get expand and collapsed callback.
Use this below snippets code,
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});
This will collapse the search action item when its focus is lost:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main_menu, menu);
searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// be sure to use 'setOnQueryTextFocusChangeListener()'
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean newViewFocus)
{
if (!newViewFocus)
{
//Collapse the action item.
searchItem.collapseActionView();
//Clear the filter/search query.
myFilterFunction("");
}
}
});
return super.onCreateOptionsMenu(menu);
}
This is the only way I've found to successfully collapse the search item action view when hitting the back button.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With