Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change android SearchView text

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); 
like image 488
Khaled Annajar Avatar asked Jan 20 '13 16:01

Khaled Annajar


People also ask

How to make SearchView in Android?

To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.

How do I get text from SearchView?

To get text of SearchView , use searchView. getQuery() .


2 Answers

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); 
like image 67
Khaled Annajar Avatar answered Sep 21 '22 18:09

Khaled Annajar


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(); 
like image 41
kunemata Avatar answered Sep 21 '22 18:09

kunemata