Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to listen to keyboard search button in searchView

Tags:

I have a SearchView. When the user clicks on the keyboard search button, I need to make a server call. What does the code for the listener look like? I am thinking I have to use OnClickListener. But the internal code for knowing it's the search button, I am not sure how to determine that.

like image 730
learner Avatar asked Oct 10 '14 19:10

learner


People also ask

How do I get text from SearchView?

To get text of SearchView , use searchView. getQuery() .

How to make search View 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.


1 Answers

I have done like this

the onQueryTextSubmit is the method you are looking for.

set setOnQueryTextListener on your search view.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem searchItem = menu.findItem(R.id.search_city);
    searchView = (SearchView) searchItem.getActionView();
    searchView.setQueryHint("Search View Hint");

    searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextChange(String newText) {
            //Log.e("onQueryTextChange", "called");
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {


            // Do your task here

            return false;
        }

    });

    return true;
}

hope this help

like image 94
Qadir Hussain Avatar answered Sep 25 '22 08:09

Qadir Hussain