2

I have implemented the following code but my SearchActivity::OnCreate is not getting called. Can someone please tell me what I'm missing?

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kace.SearchTest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="14" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <meta-data android:name="android.app.default_searchable" android:value=".SearchActivity"/>
        <activity
            android:name=".Search_testActivity"
            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=".SearchActivity" android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.SAMPLE_CODE" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH"/>
            </intent-filter>
            <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
        </activity>
    </application>

</manifest>

/res/xml/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/description" >
</searchable>

SearchActivity.java:

public class SearchActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction()))
        {
            String query = intent.getStringExtra(SearchManager.QUERY);
            int q;
            q = 10;
        }
    }

    @Override
    protected void onNewIntent(Intent intent)
    {
        // TODO Auto-generated method stub
        super.onNewIntent(intent);
    }

}
3
  • you have put multiple android.intent.action.MAIN,are you intentionally putting it, that means you have 2 starting activity
    – Sarim Sidd
    Commented May 22, 2012 at 15:40
  • Hi RiNxX, With or without the android.intent.action.MAIN, it's not functional. I copied this from Android Samples in the SDK... Commented May 22, 2012 at 18:10
  • possible duplicate of Cannot get searchview in actionbar to work
    – Clint
    Commented Jul 3, 2014 at 22:09

4 Answers 4

15

Android guides explicitly says:

// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

Which is in your case NOT TRUE. Because you don't want to search in MainActivity but in SearchableActivity. That means you should use this ComponentName instead of getComponentName() method:

ComponentName cn = new ComponentName(this, SearchableActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
1
  • 1
    Android should use your second snippet as that seems to work in all cases. Commented Sep 30, 2014 at 14:56
0

try this :

Update your AndroidManifest.xml as:

    <activity
        android:name=".Search_testActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
                <meta-data android:name="android.app.default_searchable" android:value=".SearchActivity"/>
    </activity>
    <activity android:name=".SearchActivity" android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.SAMPLE_CODE" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
                   android:resource="@layout/searchable" />
    </activity>
</application>

put searchable layout in layout/ instead of /res/xml/: layout/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/description" 
    android:searchMode="showSearchLabelAsBadge"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
    android:voiceLanguageModel="free_form"
    android:voicePromptText="@string/search_invoke"
    android:searchSuggestSelection=" ? "
/>

For more working example see my answer in this post

And

You can download working example from SearchWidgetExample

2
  • Thank Imran for the response. However, making your changes still does not solve my problem :( I did download the SearchWidgetExample and it did work correctly. However when I put into the main.xml layout a SearchView widget, the ResultActivity::onCreate java method never got called. Which is what I'm seeing with my app. Commented May 22, 2012 at 18:03
  • One more thing: I am using SDK level 14 and running on my Nexus phone with Android 4.0.2. Not sure if this helps... Commented May 22, 2012 at 18:11
0

Found the answer! In the onCreate method of my main activity (Not the search activity), I added the following lines:

SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());

SearchView sv = (SearchView)findViewById(R.id.searchView1);
sv.setSearchableInfo(searchableInfo);
1
0

Try adding default_searchable setting under your "source" activity:

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

In short, you need default_searchable setting for the "source" activity and searchable setting for the "search result" activity (your SearchActivity class).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.