I\'m developing an application where the user presses the \"Search\" icon in the ActionBar
and a SearchView
is made visible at the top of the scree
If you're using it in layout, you can call
mSearchView.onActionViewExpanded()
<item
android:id="@+id/search"
android:icon="@drawable/ic_search"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always"/>
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setQueryHint("Search the customer...");
searchView.setSearchableInfo(Objects.requireNonNull(searchManager).getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setIconifiedByDefault(false);
searchView.requestFocus();
}
<pre>
<item
android:id="@+id/search"
android:icon="@drawable/ic_search"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always"/>
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setQueryHint("Search the customer...");
searchView.setSearchableInfo(Objects.requireNonNull(searchManager).getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setIconifiedByDefault(false);
searchView.requestFocus();
}
</pre>
If you want to have it iconifiedByDefault
, this worked for me. setFocusable
and setIconified
are needed.
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setIconifiedByDefault(true);
searchView.setFocusable(true);
searchView.setIconified(false);
searchView.requestFocusFromTouch();
Update: If you are Using android.support.v7.widget.SearchView
the behaviour us very different. clearFocus
is needed if you don't want the keyboard pop-up all the time. For some reason the menu is recreated all the time, when using appcompat.
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setIconified(false);
searchView.clearFocus();
If you are using inside an activity you need to use
view.onActionViewExpanded();
if you are using inside menu options you need to use
MenuItem.expandActionView();
Note: it works only for SearchView
these two situations are worked for me.
You can use the SearchView#setIconified()
method on a SearchView handle in your Java code. More here:
http://developer.android.com/reference/android/widget/SearchView.html#setIconified(boolean)
You can also make use of SearchView#setIconifiedByDefault()
. More info here:
http://developer.android.com/reference/android/widget/SearchView.html#setIconifiedByDefault(boolean)
Call expandActionView()
on the menuItem.
menuItem.expandActionView()