Android - SearchView on toolbar not expanding

I'm trying to add SearchView to my toolbar, I added an icon, but when I click it, it does not expand.

Here is the code:

main_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.app.try.MainActivity"> <item android:id="@+id/search" android:title="@string/search" android:icon="@drawable/abc_ic_search_api_mtrl_alpha" app:showAsAction="always" android:actionViewClass="android.support.v7.widget.SearchView"/> </menu> 

MainActivity.java

 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater=getMenuInflater(); inflater.inflate(R.menu.menu_main,menu); MenuItem searchItem = menu.findItem(R.id.search); SearchManager searchManager= (SearchManager)getSystemService(Context.SEARCH_SERVICE); SearchView searchView=null; if (searchItem!=null) { Log.d("createOptionMenu","search item not null"); searchView = (SearchView) searchItem.getActionView(); } if (searchView!=null) { Log.d("createOptionMenu","search view not null"); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); }else{ Log.e("createOptionMenu","search view null"); } return true; } 

(this always shows the log message "search as null")

AndroidManifest.xml

 ... <activity android:name=".MainActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name" android:screenOrientation="portrait" > <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity> <activity android:name=".SearchResultActivity" android:label="@string/title_activity_search_result" > <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> ... 

searchable.xml

 <?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_name" android:hint="HINTTT" /> 

I tried several options with expandActionView() or setIconified(false) , but not for those that work for me.

+7
android searchview
source share
3 answers

You must use MenuItemCompat

 mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); 

and to collapse SearchView use

 MenuItemCompat.collapseActionView(searchItem); 

and make sure you change android:actionViewClass to app:actionViewClass

+23
source share

I had the same problem. I just solved mine with a little tweak. android:actionViewClass="android.support.v7.widget.SearchView" change android:actionViewClass="android.support.v7.widget.SearchView"

to app:actionViewClass="android.support.v7.widget.SearchView"

+1
source share

In MainActivity you need to extend ActionbarActivity

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:appcompat="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/tools"> <item android:id="@+id/menu_search" android:title="@string/menu_search" appcompat:actionViewClass="android.support.v7.widget.SearchView" appcompat:showAsAction="always"/> 

MainActivity.java

 public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); // Inflate menu to add items to action bar if it is present. inflater.inflate(R.menu.menu_main, menu); // Associate searchable configuration with the SearchView SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName())); return true; } 

}

-2
source share

All Articles