user2141833 user2141833 - 22 days ago 10
Android Question

submit a query when click on search button in keyboard

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

Answer

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;
}
Comments