Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searchable Configuaration Returning Null For the SearchView

I have created a search interface but I can't seem to connect the SearchView in the ActionBar to my searchable configuration file. When I try to retreive it in my onCreateOptionsMenu method SearchManager.getSearchableInfo(getComponentName() returns null. Could someone help me out 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 Activity 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>

Searchable 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 menu xml file and I tried cleaning the project. None of them seem to do the trick.

like image 956
Tanuj Nayak Avatar asked Dec 27 '22 12:12

Tanuj Nayak


1 Answers

Ok never mind. After some research I found out that the android documentation for creating a search interface isn't complete: some things were left out like

1) No string should be hardcoded in the searchable conifguration

2) I should have this code in the <activity> which does the search:

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

3) I should also have this code in the <activity> where I want the SearchView to show up in the ActionBar

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

For the sake of completion: Here is my manifest file which 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>
like image 156
Tanuj Nayak Avatar answered Mar 05 '23 08:03

Tanuj Nayak