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();
}
You can add listener for this as :
MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do whatever you need
return true; // KEEP IT TO TRUE OR IT DOESN'T OPEN !!
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do whatever you need
return true; // OR FALSE IF YOU DIDN'T WANT IT TO CLOSE!
}
});
You only need to put the "collapseActionView" attribute in the menu layout
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_item_search"
android:title="@string/search"
android:iconifiedByDefault="true"
android:icon="@drawable/ic_action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/> <--this one
</menu>
That will give you the functionality you look for all by itself.
Try this:
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.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) {
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.
In menifest file
For handling back button of Action Bar - Activity declaration part first of all need to define Parent Activity.
Like-
android:name="com.android.unum.ui.SelfServiceActivity"
android:label="@string/self_service"
android:parentActivityName="com.android.unum.ui.DashboardActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.android.unum.ui.DashboardActivity" />
Edit onOptionsItemSelected method thats inside activity
NavUtils.navigateUpFromSameTask(this);
//public static void navigateUpFromSameTask (Activity sourceActivity)
//sourceActivity will be finished by this call.
Note: This method should only be used when sourceActivity and the corresponding parent are within the same task.
For More Info :-
http://developer.android.com/reference/android/support/v4/app/NavUtils.html
Like
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}