Searchable Search Function Returns Zero for SearchView

I created a search interface, but I cannot connect SearchView in ActionBar to my searchable configuration file. When I try to find it in my onCreateOptionsMenu SearchManager.getSearchableInfo(getComponentName() method, it returns null. Can someone help me with this?

Settings.onCreateOptionsMenu (menu)

 @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.activity_main, menu); SearchManager sm = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView sv = (SearchView) menu.findItem(R.id.menu_search).getActionView(); SearchableInfo info = sm.getSearchableInfo(getComponentName()); //This variable is returned as null sv.setSearchableInfo(info); sv.setIconifiedByDefault(false); sv.setSubmitButtonEnabled(true); return true; } 

Settings in AndroidManifest.xml

 <activity android:name=".Settings" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity> 

Search activity in AndroidManifest.xml

  <activity android:name=".ImageSearch" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> </activity> 

I have the header <?xml version="1.0" encoding="utf-8"?> In my XML menu file and I tried to clear the project. None of them seem to do the trick.

+4
source share
3 answers

Good, never mind. After some research, I found out that the Android documentation for creating the search interface is not complete: some things were omitted, like

1) No line should be hard-coded in search confection

2) I have to have this code in <activity> that does the search:

  <meta-data android:name="android.app.searchable" android:resource="@xml/your search configuration file name" /> 

3) I should also have this code in <activity> , where I want the SearchView display in the ActionBar

  <meta-data android:name="android.app.default_searchable" android:value=".ActivityWhichCarriesOutTheSearch" /> 

To complete: here is my manifest file that works:

 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="false" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" /> </activity> <activity android:name=".SearchActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> </activity> </application> 
+9
source

If you extend ActionBarActivity in mainactivtity , and then in your menu_item you should give your search query a type

 "android.support.v7.widget.SearchView" 

I also got the same error as I decided: thanks ....

 <item android:id="@+id/search" android:title="Search for coupons,deals,stores,etc....." android:icon="@drawable/ic_search" app:showAsAction="always" app:actionViewClass="android.support.v7.widget.SearchView" /> 
+1
source

I use the android: launchMode = "singleTop" property when I havn't an activity that does a search. And the metadata looks like this:

 <activity android:name=".Activities.Account" android:label="  " android:screenOrientation="portrait" android:launchMode="singleTop" > <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> </activity> 
0
source

All Articles