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

3

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?

Baroque answered 16/8, 2012 at 13:12 Comment(0)
B
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

Baroque answered 16/8, 2012 at 14:14 Comment(5)
I can't at the moment due some kind of limit (tomorrow i'll be able to accept it).Miter
I had a similar problem. You can put the <meta-data> in the main activity, and it will still work. Just a FYI.Spadework
@IndrekKõue you saved my life i'm stuck on this for almost 5hrsLashonlashond
Thanks! This could've taken a long time to figure out :)Daffodil
I have a problem. While this solution works, it replaces the search key in the keypad with an arrow.Rembrandt
S
0

You should override onOptionsItemSelected and probably onSearchRequested in your activity.

Substage answered 16/8, 2012 at 13:15 Comment(4)
I'm using Search Widget not Search DialogMiter
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).Miter
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?Substage
Sorry my bad, by dialog I meant the search widget on the action bar.Miter
S
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.

Serve answered 19/11, 2015 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.