Android how to focus ActionBar searchView

max4ever picture max4ever · Apr 10, 2012 · Viewed 43k times · Source

Hello i need to put some text inside the searchView and focus/expand the searchView widget.

this is what i tried but it doesn't work

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
...
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setQuery(this.filtro, false);
    searchView.setFocusable(true);
    searchView.requestFocus();
    searchView.requestFocusFromTouch();

Answer

farmer.chs picture farmer.chs · Apr 25, 2012

Try using searchView.setIconified(false). This worked for me:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ActionBar actionBar = getActionBar();
    SearchView searchView = new SearchView(this);
    actionBar.setCustomView(searchView);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    searchView.setQuery("test",true);
    searchView.setFocusable(true);
    searchView.setIconified(false);
    searchView.requestFocusFromTouch();
}