112

There is this widget for the ActionBar which called 'SearchView'. When it's not in use, it looks like this:

enter image description here

And when it's in use, it looks like this:

enter image description here

I want (programmatically of course) to open the searchview (make it "in use").

I tried several functions such as:

SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setOnQueryTextListener(this);

    searchView.performClick();
    searchView.requestFocus();

But none of those worked...

The SearchView in the XML:

<item android:id="@+id/menu_search"
      android:title="Search"
      android:icon="@drawable/ic_action_search"
      android:showAsAction="ifRoom|collapseActionView"
      android:actionViewClass="android.widget.SearchView" />
Cœur
  • 37,241
  • 25
  • 195
  • 267
Eli Revah
  • 3,626
  • 7
  • 27
  • 33

9 Answers9

278

Expand the SearchView with

searchView.setIconified(false);

and collapse it with

searchView.setIconified(true);

You need to change the value of android:showAsAction from ifRoom|collapseActionView to always. The SearchView's attribute android:iconifiedByDefault should be true, which is the default value, otherwise the user can not collapse the SearchView after it was expanded programmatically.

Matthias Robbers
  • 15,689
  • 6
  • 63
  • 73
  • 15
    Hello, a keyboard pops-up but the SearchView does not open. – Eli Revah Jan 09 '13 at 13:21
  • OK, I'm a little surprised, works for me. `searchView.onActionViewExpanded()` should work as well. If not, can you show your menu.xml? – Matthias Robbers Jan 09 '13 at 13:29
  • 1
    Hello, this also didn't work. I updated the post with the XML source code. – Eli Revah Jan 09 '13 at 13:35
  • i have one query, how to show the caret besides the app icon when the `searchView` expands and hide the caret when `searchView` collapses? – Ram Patra Feb 11 '14 at 18:33
  • I tried opening the searchView again during `onResume()` with the provided method, which was too early. The OptionMenu Items did not settle at that time. Using `onPrepareOptionsMenu()` worked perfectly for me. – Capricorn Nov 11 '15 at 00:51
  • 7
    Man you are my f*king hero – Eliseo Ocampos Jul 14 '17 at 16:50
  • 2
    showAsAction="always" will display an ugly fullscreen search bar though, after rotation – Francesco Gabbrielli Feb 25 '18 at 01:01
  • 2
    `showAsAction="always"` makes the search icon stay on the action bar when is expanded, which doesn't resemble to appearance when focusing the search manually. – User Apr 30 '18 at 08:19
  • Don't use this and use the solution provided in @qbait's answer (`searchMenuItem.expandActionView()`). Setting `showAsAction="always"` causes several problems (as commented above). – User Apr 30 '18 at 08:25
81

Try to call expandActionView() on MenuItem, not onActionViewExpanded() on ActionView.

It works for me.

MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
searchView = (SearchView) searchMenuItem.getActionView();
searchMenuItem.expandActionView();
jakub
  • 3,576
  • 3
  • 29
  • 55
  • 10
    Works perfect, but only for API > 14. For earlier API you can use this: MenuItemCompat.expandActionView(searchMenuItem); – Andrei Aulaska May 29 '14 at 08:21
  • This is the correct solution - The selected one forces you to set `showAsAction: always` which causes some issues. – User Apr 30 '18 at 08:24
13

If you want to use support library only when necessary, do this

    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    if (Utils.hasIceCreamSandwich())
        searchMenuItem.expandActionView();
    else MenuItemCompat.expandActionView(searchMenuItem);

else simply do this

    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    MenuItemCompat.expandActionView(searchMenuItem);
Olayinka
  • 2,813
  • 2
  • 25
  • 43
6

I know this is late but

Try calling expandActionView() to open it and collapseActionView() to close it. You can call requestFocus() on the actual Action View via getActionView() to give the search view focus :)

James Campbell
  • 3,511
  • 4
  • 33
  • 50
6

For androidx.appcompat.widget.SearchView,

searchView.setIconifiedByDefault(true)   // didn't work

searchMenuItem.expandActionView()  // didn't work

MenuItemCompat.expandActionView(searchMenuItem) // didn't work

searchView.onActionViewExpanded()  // didn't work

The following worked for me,

searchView.findViewById<View>(R.id.search_button).performClick()
Aziz
  • 1,976
  • 20
  • 23
2

Expanding the answer of Matthias Robberts:

I wanted a list fragment to keep it's search value and set it after user returns from other fragment.

if (myViewModel.filterTextSaved.isNotEmpty()) { // Kotlin, storing state in ViewModel
    searchItem.expandActionView() // needs to come first, otherwise empty text
    theTextArea.setText(courseViewModel.filterTextOnClick)
}

and for the menu I keep always|collapseActionView, otherwise it stays open when user deletes the text.

Gunnar Bernstein
  • 6,074
  • 2
  • 45
  • 67
1

I was searching for a solution to expand SearchView programmatically because I need to restore expand state. No one solution was worked for me. Except using Handler().post(). I know it's not a good practice to use post(). But I'm working on legacy project and this workaround is acceptable for me.

Initialize variables block

val searchMenuItem = menu.findItem(R.id.menu_search_item)
val searchView = searchMenuItem.actionView as SearchView

How to expand SearchView:

Handler().post {
   searchMenuItem.expandActionView()
}

Reason why post() helps: if your app is not well written it may doing too much work on UI thread. post() ensures that your block of code will be executed when UI thread will be available for execution. Which means if you are using this workaround you can notice small delay between SearchView expanding

0

To open up a searchView keeping the close_button in the end use this in onCreate:-

searchView.findViewById(R.id.search_button).performClick();
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
0

If you are using Material Search View com.google.android.material.search.SearchView,

you can show and hide the `` with

searchView.show() // expands SearchView

searchView.hide() // collapses SearchView
All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111