16

I'm working around with a searchable action bar. And I gotta a problem that the action bar doesn't react with the searchable activity. THAT IS, I enter something in the action bar's searchable textedit then clicked the submit but nothing happened. The debug tracking shows that the code in my searchresultactivity is never executed. So I'm wondering if there's something wrong in searchable configuration. Well this time I think I've explained the problem clearly and I don't expect any negative votes without any words even spits!

I followed the instruction of developer's document beginning with the manifest.xml, the meta-data is added in the searchresult activity:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".GermanDictionaryActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SearchResultsActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter> 
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
</application>

then I initialized the searchview with my menu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.srhbar, menu);

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    mSrhView = (SearchView) menu.findItem(R.id.search).getActionView();
    mSrhView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    mSrhView.setSubmitButtonEnabled(true);

    return true;
}

And the searchable item in menu:

<item android:id="@+id/search"
    android:title="@string/srh_title"
    android:showAsAction="collapseActionView|ifRoom"
    android:actionViewClass="android.widget.SearchView" />

Any answer related is appreciated!

  • where u r calling `onSearchRequested()` ? – ρяσѕρєя K Mar 29 '12 at 11:55
  • see this example maybe helpful :[SearchableDictionary](http://android.toolib.net/resources/samples/SearchableDictionary/index.html) – ρяσѕρєя K Mar 29 '12 at 12:05
  • 2
    I gotta the problem solved shortly after post this question, but thank you anyway. That's because I didn't add default searchable meta-data in my main activity. my program is a little bit different from the sample while mine split the search function and main activity into two classes. And, onSearchRequested() function will make the bar-embedded searchable textedit as an independent view displayed solely. –  Mar 29 '12 at 12:18
  • Why is onSearchRequested() required? – IgorGanapolsky Sep 12 '13 at 18:00

1 Answers1

20

I solved the problem by changing this line:

searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(getApplicationContext(), SearchResultsActivity.class)));
einverne
  • 6,454
  • 6
  • 45
  • 91
  • I guess this answer and also this question saved me too! Thanks a lot! It is so good to know that there is always someone with similar problems like yours who you can solve it with. Thank you again and again! – Salivan Oct 02 '14 at 13:30
  • 2
    This should be in the android documentation. Saved my time. Thanks. – Prakash Jul 05 '18 at 21:09