3

I've been following this tutorial http://developer.android.com/training/search/setup.html and also checking in Stackoverflow but I can't find the solution y guess both problems are related.

Here is my searchable.xml under xml folder in res:

<?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" >
</searchable>

This is my menu with the search button:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_search"
    android:title="@string/search_title"
    android:icon="@drawable/ic_search_white_24dp"
    app:showAsAction="collapseActionView|ifRoom"
    app:actionViewClass="android.support.v7.widget.SearchView" />

Here I associate the searchable configuration:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    return true;
}

This is the manifest file:

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

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:name=".MainApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data android:name="android.app.default_searchable"
        android:value=".activities.SearchResultsActivity"/>
    <activity
        android:name=".activities.MainActivity"
        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=".activities.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>

With this code I get the user interface working but it doesn't start the searchable activity and it doesn't associate the searchable configuration, here is how it looks after compiling:

enter image description here

As you can see the user interface seems to work, I already checked that the names in searchable.xml are string resources, i added

<meta-data android:name="android.app.default_searchable"
        android:value=".activities.SearchResultsActivity"/>

which isn't in the Google tutorial, I don't know what I'm missing here.

dan87
  • 333
  • 3
  • 16

2 Answers2

3

I found the solution, I had to use the fully qualified class name instead of .activities.SearchResultsActivity

moDev
  • 5,248
  • 4
  • 33
  • 63
dan87
  • 333
  • 3
  • 16
0

The fully qualified name didn't make it work for me. For me, the fix was to ensure the following:

  1. Setting a new ComponentName with applicationContext and the "Searchable Activity"
  2. The searchable meta-data tag goes on the "Searchable Activity", not the Activity that has the SearchView like the docs state.
  3. Making sure your searchable.xml has both a label and a hint at a minimum
DroidClicketyClacker
  • 2,057
  • 1
  • 10
  • 10