I noticed a couple days ago that inputting text in my SearchView
doesn\'t show up. Maybe this problem started longer ago and I hadn\'t noticed it, but I know th
I had the same issue, and for me it was because I had set my toolbar height to wrap_content. Try giving it a size, like this:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="@style/ToolbarTheme"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:layout_scrollFlags="scroll|enterAlways"
/>
This is the code I use for my recent project and it's work fine:
First define search_menu.xml
(make sure use android supportV7
) :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/ic_search_gray"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
Then define in your onCreateOptionMenu
of your Activity:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(getApplicationContext(), SearchActivity.class)));
searchView.setMaxWidth(Integer.MAX_VALUE);
MenuItemCompat.expandActionView(menu.findItem(R.id.search));
searchView.setIconifiedByDefault(true);
searchView.setIconified(false);
Also my Toolbar:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_lightest"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:contentInsetStartWithNavigation="0dp" />
Add meta-data to your AndroidManifest.xml
:
<activity
android:name=".ui.activity.SearchActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"
android:value=".ui.activity.SearchActivity" />
</activity>
Final result: