Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting hint text for search widget when used as part of menu

Tags:

java

android

I have a menu item as part of my action bar menu, and I am setting the action view class to the search widget like so:

   <item android:id="@+id/menu_search"
        android:title="Search"
        android:icon="@drawable/ic_menu_search"
        android:showAsAction="always"
        android:actionViewClass="android.widget.SearchView" />

This makes the search widget pop out when it is clicked - however, I want it to always be visible, and I also want to be able to set the query hint etc.

When I attempt to call SearchView searchItem = (SearchView) menu.findItem(R.id.menu_search) to get a reference to it, it throws an error as the item cannot be cast to a SearchView.

like image 878
jcvandan Avatar asked Sep 17 '11 18:09

jcvandan


People also ask

How do I add hint in search?

Using the XML configuration. If the hint's desired to be visible in the Non-Active State of SearchView set android:iconifiedByDefault to "false" . If it's undesired set android:iconifiedByDefault to "true" . This is the answer without creating a function or code in the Activity. Just add this in the layout.

How to implement search view in android?

SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class. For making search field visible, SearchView uses setIconifiedByDefault(false) method.


2 Answers

You can set hint text to searchview using the api setQueryHint (CharSequence hint)

SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setQueryHint("Query Hint");

Refer the documentation

like image 193
Raneez Ahmed Avatar answered Sep 17 '22 17:09

Raneez Ahmed


This will make the ActionView search box always show, by default:

searchView.setIconifiedByDefault(false);

This tells the search box to always be expanded. Iconified means that you need to tap the icon to make the search box appear.

like image 44
Jerome Avatar answered Sep 20 '22 17:09

Jerome