104

Is there a setText() method for SearchView or something like that? I try to set the search text in the searchView like this but there is no method for like this.

searchView.setText(searchToken);
Khaled Annajar
  • 15,542
  • 5
  • 34
  • 45

5 Answers5

186

After wondering and trying I found out that there is a method in the API named setQuery() which you set the searchView text and you may choose to submit the search or not using the boolean parameter.

searchView.setQuery(searchToken, false);
Khaled Annajar
  • 15,542
  • 5
  • 34
  • 45
  • 3
    Even after setting the boolean parameter false it still queries again.Any suggestions would help. – Raj Trivedi Feb 06 '14 at 05:42
  • I've done this, but setting the 2nd param to `true` so the query gets executed as well, but note that though the query gets executed well, the text box stays blank, until when manually given focus. How would I ensure the auto-set value gets displayed automatically too? – JWL Mar 06 '18 at 07:16
  • 1
    it's OK. second argument is "submit" – roghayeh hosseini Oct 23 '18 at 15:16
41

You can use setQuery() to change the text in the textbox.

However, setQuery() method triggers the focus state of a search view, so a keyboard will show on the screen after this method has been invoked.

To fix this problem, just call searchView.clearFocus() after setQuery() method to unfocus it and the keyboard will not show on the screen.

Example:

String suggestWord = intent.getDataString();
searchView.setQuery(suggestWord, false);
searchView.clearFocus();
kunemata
  • 726
  • 1
  • 6
  • 8
17

this is what worked for me

    MenuItem sItem = menu.findItem(R.id.search);
    SearchView searchView = (SearchView) sItem.getActionView();

    sItem.expandActionView();
    searchView.setQuery("your text", false);
Wowo Ot
  • 1,362
  • 13
  • 21
10

If you want pre-fil your SearchView with some text searchView.setQuery(text, false) would not work out of the box. the reason is when SearchView gets expanded

searchView.onActionViewExpanded()

get called which calls searchView.setText("") and clears any text we have set.

Solution is to set an Expand listener and set the pre-fil text once the SearchView is expanded

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.main, menu)
    val searchView = menu.findItem(R.id.action_search).actionView as SearchView
    menu.findItem(R.id.action_search).setOnActionExpandListener(object : MenuItem.OnActionExpandListener {
        override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
            // it is important to call this before we set our own query text.
            searchView.onActionViewExpanded()
            searchView.setQuery("Prefil Text", false)
            return true
        }

        override fun onMenuItemActionCollapse(item: MenuItem?) = true
    })
    return true
}
Atiq
  • 14,435
  • 6
  • 54
  • 69
  • Great answer. But beware of **searchView.onActionViewExpanded()** and **searchView.setQuery()** _both_ triggering **onQueryTextChange()** if you have deployed **searchView.setOnQueryTextListener**. The first trigger may very well empty the String about to be supplied to the second trigger. – Bad Loser Oct 17 '20 at 03:32
-1

If you want the searchView to expand and it can be shrink back to an icon after the user tap x (close), the way is:

searchView.isIconified = false // Expand it
searchView.setQuery("your text here", false) // true if you want to submit, otherwise false
searchView.clearFocus() // so the keyboard is not show up.

NOTE: If you expand it using:

searchView.onActionViewExpanded()

searchView will not shrink back after user tap x (close)

Putra Hardjono
  • 567
  • 1
  • 4
  • 4