submit a query when click on search button in keyboard
Asked Answered
R

4

6

I have search view in my fragment. when I click on it , keyboard is open and I can type text. I want when I click on search button in keyboard , my query send to my server and get result but I don't know how get search event. any solution?enter image description here

Rutland answered 9/7, 2014 at 19:13 Comment(0)
P
10

You have to extend OnQueryTextListener, attach the listener and implement onQueryTextSubmit.

Example:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    searchView = (SearchView) menu.findItem(R.id.mActionSearch).getActionView();
    searchView.setOnQueryTextListener(this);

    return true;
}   

@Override
public boolean onQueryTextSubmit(String query) {
    //Do something here
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    return false;
}
Pitchblack answered 9/7, 2014 at 19:19 Comment(2)
stackoverflow doesn't allow me to accept your answer until 5 minute passed my friend ;)Rutland
This still works great. I wonder if there's a better solution now after 3 years.Diao
E
0

Pozzo Apps Answer is right

but for api below 11 and compat library you can use something like this :

    MenuItem search_menu=(MenuItem)menu.findItem(R.id.action_search);
    SearchView searchView =(SearchView)MenuItemCompat.getActionView(search_menu);
Exocentric answered 3/5, 2015 at 10:32 Comment(0)
M
0

You can also apply setOnKeyListener on search view like as below:

searchview.setOnKeyListener(new View.OnKeyListener(
{
   Public boolean onKey(View v, int keyCode, KeyEvent event)
     {
        if(event.getAction() == KeyEvent.ACTION_DOWN)
        {
            switch(keyCode)
            {
                 Case KeyEvent.KECODE_ENTER:
                       // Apply action which you want on search key press on keypad
                        return true;
                 default:
                       break;
             }
          } return false;
       }
 });
Macgregor answered 11/11, 2016 at 10:31 Comment(0)
E
0

You have to add new OnQueryTextListener, and implement onQueryTextSubmit. This also works in a fragment.

Example:

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

    SearchView sv = (SearchView) menu.findItem(R.id.action_search).getActionView();
    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            //Do something here
            Toast.makeText(getActivity(), "Search: " + query, Toast.LENGTH_SHORT ).show();
            return false;
        }

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

    super.onCreateOptionsMenu(menu,inflater);
}
Electrical answered 21/2, 2018 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.