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
.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With