23

I'm developing search widget interface based on official tutorial: http://developer.android.com/guide/topics/search/search-dialog.html

Problem: My SearchableActivity doesn't get triggered when I enter my query and press Ok/enter.

Manifest for SearchableActivity:

<activity android:name="SearchableActivity" android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
</activity>

xml/searchable.xml

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

Main activity lifecycle method which adds icons to the action bar (works fine):

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);

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

    // Do not iconify the widget;expand it by default
    searchView.setIconifiedByDefault(false);

    return true;
}

SearchableActivity.java

public class SearchableActivity extends ListActivity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("MY", "search activity triggered");
   }

}

Note: The search widget appear on the action bar and I can insert data, but pressing the OK/Enter doesn't take me to the SearchableActivity (doesn't trigger onCreate of the SearchableActivity).

Am I missing something or is the official tutorial flawed?

Indrek Kõue
  • 6,449
  • 8
  • 37
  • 69

3 Answers3

60

Problem solved: the tutorial seems to missing one important part: <meta-data android:name="android.app.default_searchable" android:value=".MySearchActivityName" /> has to be added inside <application> tags in manifest to get the search widget working correctly.

EDIT- Also a hint to solving a problem when the actionbar search is not triggered on data posting (no error given whatsoever and documentations hasn't got a word about this limitation): in searchable.xml file android:hint and android:label attributes MUST be references to strings in strings.xml. Source

Community
  • 1
  • 1
Indrek Kõue
  • 6,449
  • 8
  • 37
  • 69
0

You should override onOptionsItemSelected and probably onSearchRequested in your activity.

devmiles.com
  • 9,895
  • 5
  • 31
  • 47
  • 1
    I'm using [Search Widget](http://developer.android.com/guide/topics/search/search-dialog.html#UsingSearchWidget) not [Search Dialog](http://developer.android.com/guide/topics/search/search-dialog.html#InvokingTheSearchDialog) – Indrek Kõue Aug 16 '12 at 13:23
  • I don't have it overriden, the search widget tutorial doesn't state that it needs to be overriden. Also this part seems to be working (the search dialog opens when i tap the search icon on the action bar and I can enter and delete text). – Indrek Kõue Aug 16 '12 at 13:30
  • You mention search dialog appearing, it should not in case of a widget, the widget is built into the action bar, so what's the case? – devmiles.com Aug 16 '12 at 13:43
  • Sorry my bad, by dialog I meant the search widget on the action bar. – Indrek Kõue Aug 16 '12 at 13:55
0

If "xml/searchable.xml" file is not correctly formatted (things such as "searchable" tag not in all lower case), there is no error message returned during execution and the "SearchableActivity" doesn't get invoked.

Naveen Saini
  • 19
  • 1
  • 3