Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search widget on action bar doesn't trigger my search activity

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?

like image 367
Indrek Kõue Avatar asked Aug 16 '12 13:08

Indrek Kõue


1 Answers

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

like image 95
Indrek Kõue Avatar answered Oct 16 '22 08:10

Indrek Kõue