acanidio
acanidio

Reputation: 13

SearchView doesn't start the SearchableActivity

I want to add a SearchView to my ActionBar, so I've done as in Google Tutorial reported here.

AndroidManifest.xml

<activity
        android:name="it.polimi.dima.sound4u.activity.SoundSearchActivity"
        android:label="@string/title_activity_sound_search" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    </activity>
    <activity
        android:name="it.polimi.dima.sound4u.activity.MyGiftsActivity"
        android:label="@string/title_activity_my_gifts" >
        <meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable" />
    </activity>

searchable.xml

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

MyGiftsActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my_gifts, menu);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search:
            onSearchRequested();
            return true;
        case R.id.action_logout:
            doLogout();
            return true;
        case R.id.action_settings:
            return true;
    }
    return super.onOptionsItemSelected(item);
}

my_gifts.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

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


    <item android:id="@+id/action_logout"
          android:title="@string/logout_label"
          android:showAsAction="ifRoom" />

    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:orderInCategory="100"
          app:showAsAction="never" />
</menu>

SoundSearchActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sound_search);
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        Log.w(TAG_LOG, query);
    }
}

The SearchView is correctly created and can switch from the Icon mode to the TextView mode but when I push the Search Button on the keyboard the new SoundSearchActivity doesn't start. Anyone can help me?

Upvotes: 0

Views: 305

Answers (1)

PieterAelse
PieterAelse

Reputation: 3678

Try this in your manifest:

  • Add a meta-data pointing to the SearchActivity in the tag of the Activity where search is started
  • Move the searchable tag to the SearchAvtivity
  • Add launchMode="singleTop" to the SearchActivity to make sure there's only one instance of it at top of the stack. New search intents will come in trough onNewIntent which you already implemented.

            <meta-data android:name="android.app.searchable"
                       android:resource="@xml/searchable" />
        </activity>
        <activity
            android:name="it.polimi.dima.sound4u.activity.MyGiftsActivity"
            android:label="@string/title_activity_my_gifts" >
    
            <meta-data android:name="android.app.default_searchable"
                       android:value="it.polimi.dima.sound4u.activity.SoundSearchActivity"/>
    

The initial guide by Google (found here) didn't give me a working SearchView either, but making theses changes did.

Upvotes: 2

Related Questions