Example usage for android.widget SearchView setOnQueryTextListener

List of usage examples for android.widget SearchView setOnQueryTextListener

Introduction

In this page you can find the example usage for android.widget SearchView setOnQueryTextListener.

Prototype

public void setOnQueryTextListener(OnQueryTextListener listener) 

Source Link

Document

Sets a listener for user actions within the SearchView.

Usage

From source file:com.cypress.cysmart.CommonFragments.ProfileScanningFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();/*from  w  ww.ja  v  a 2 s  . com*/
    inflater.inflate(R.menu.global, menu);
    MenuItem item = menu.findItem(R.id.graph);
    MenuItem log = menu.findItem(R.id.log);
    final MenuItem searchMenuItem = menu.findItem(R.id.search);
    item.setVisible(false);
    log.setVisible(true);

    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
    searchView.setIconifiedByDefault(false);
    SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            // this is your adapter that will be filtered

            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            if (searchMenuItem != null) {
                searchMenuItem.collapseActionView();
                // this is your adapter that will be filtered
                if (mLeDeviceListAdapter != null) {
                    searchEnabled = true;
                    searchDeviceList(query);
                }
            }
            return false;

        }
    };
    searchView.setOnQueryTextListener(textChangeListener);
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean queryTextFocused) {
            if (!queryTextFocused) {
                searchMenuItem.collapseActionView();
                searchView.setQuery("", false);
            }
        }
    });

    super.onCreateOptionsMenu(menu, inflater);
}

From source file:biz.bokhorst.xprivacy.ActivityMain.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    if (inflater != null && PrivacyService.checkClient()) {
        // Inflate menu
        inflater.inflate(R.menu.main, menu);

        // Searchable
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search));
        if (searchView != null) {
            searchView.setIconifiedByDefault(false);

            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override//ww w  .j ava  2  s.c  om
                public boolean onQueryTextChange(String newText) {
                    searchQuery = newText;
                    applyFilter();
                    return true;
                }

                @Override
                public boolean onQueryTextSubmit(String query) {
                    searchQuery = query;
                    applyFilter();
                    return true;
                }
            });
            searchView.setOnCloseListener(new SearchView.OnCloseListener() {
                @Override
                public boolean onClose() {
                    searchQuery = "";
                    applyFilter();
                    return true;
                }
            });
        }

        return true;
    } else
        return false;
}

From source file:net.nightwhistler.pageturner.activity.ReadingFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.reading_menu, menu);

    this.searchMenuItem = menu.findItem(R.id.search_text);

    if (this.searchMenuItem != null) {
        final com.actionbarsherlock.widget.SearchView searchView = (com.actionbarsherlock.widget.SearchView) searchMenuItem
                .getActionView();//  ww  w.  j  av  a2s .  c  om

        if (searchView != null) {

            searchView.setSubmitButtonEnabled(true);
            searchView
                    .setOnQueryTextListener(new com.actionbarsherlock.widget.SearchView.OnQueryTextListener() {

                        //This is a work-around, since we get the onQuerySubmit() event twice
                        //when the user hits enter
                        private String lastQuery = "";

                        @Override
                        public boolean onQueryTextSubmit(String query) {

                            if (query.equals(lastQuery) && searchResults != null) {
                                showSearchResultDialog(searchResults);
                            } else if (!query.equals(lastQuery)) {
                                searchResults = null;
                                lastQuery = query;
                                performSearch(query);
                            }

                            return true;
                        }

                        @Override
                        public boolean onQueryTextChange(String newText) {
                            return false;
                        }
                    });
        }
    }
}