Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle back button of Search View in android

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.?

enter image description here

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();

    }
like image 694
Android_Paradise Avatar asked Jul 15 '14 13:07

Android_Paradise


People also ask

How to set Search icon in ToolBar Android?

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.

How to use SearchView in Android?

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.

Do all Android phones have a back button?

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.


2 Answers

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;
            }
        });
like image 167
Krunal Shah Avatar answered Oct 12 '22 00:10

Krunal Shah


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.

like image 45
Sakiboy Avatar answered Oct 12 '22 00:10

Sakiboy