I've followed this Android guide in order to add a search bar to an activity. The setup looks like this:
res/menu/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/search"
android:title="@string/menu_search"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="@+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/menu_settings"
android:icon="@drawable/ic_menu_settings"/>
</menu>
res/xml/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="@string/search_hint" />
Activity entry in manifest file:
<activity
android:name="de.ivu.realtime.app.activity.MainActivity"
android:label="@string/app_name" >
<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" />
<meta-data
android:name="android.app.default_searchable"
android:value="de.ivu.realtime.app.activity.MainActivity" />
</activity>
and the code in MainActivity:
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
this.getMenuInflater().inflate(R.menu.activity_main, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(searchableInfo);
}
return true;
The call to searchManager.getSearchableInfo(getComponentName());
always returns null
. I've seen this in other questions on stackoverflow, however, none of the solutions presented there seem to apply to my case.
<?xml version="1.0" encoding="utf-8"?>
headerAny help is very much appreciated. Thanks!
Add it to the activity where you've put your SearchView
<meta-data
android:name="android.app.default_searchable"
android:value=".activity_that_handles_the_search" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
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