9

Search activity not being launched when pressing enter.The search view is shown nicely on the action bar. But when i type the search query and press enter

AndroidManifest.xml

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

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"

    >
    <activity
        android:name="com.punit.rateit.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:theme="@style/Theme.AppCompat.Light"
              android:name=".SearchPageActivity">
          <meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity" /> 
      <intent-filter>
         <action android:name="android.intent.action.SearchPage" />
      </intent-filter>
    </activity>

   <activity android:name="com.punit.rateit.SearchResultsActivity"   android:launchMode="singleTop" >
  <intent-filter>
    <action android:name="android.intent.action.SEARCH" />
     <meta-data android:name="android.app.searchable"
               android:resource="@xml/searchable"/>
    </intent-filter>
   </activity>       

    </application>

 </manifest>

Here is the Menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:com.punit.rateit="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="@+id/search"
      android:icon="@drawable/ic_search"
      android:title="@string/action_search"
      com.punit.rateit:actionViewClass="android.widget.SearchView"
       com.punit.rateit:showAsAction="ifRoom"

       />

   </menu>

The activity which shows action bar.

     public boolean onCreateOptionsMenu(Menu menu) {

     MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu,menu);
     SearchManager searchManager =
             (SearchManager) getSystemService(Context.SEARCH_SERVICE);

      SearchView searchView =
              (SearchView) menu.findItem(R.id.search).getActionView();

      searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
      searchView.setIconifiedByDefault(false);
      searchView.setSubmitButtonEnabled (true); 
     return super.onCreateOptionsMenu(menu);
 }

The SearchResult activity which should be the activity called when search submit button is pressed

public class SearchResultsActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("search", "search triggered");
    setContentView(R.layout.searchpage);

    handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
    Log.d("search", "search triggered");
    setIntent(intent);
    handleIntent(intent);
}
private void handleIntent(Intent intent)
{
     if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            Log.d("search", query);
}
  }

 @Override
 public boolean onSearchRequested() {

     Log.d("search", "search triggered");

     return false;  // don't go ahead and show the search box
 }
Puneet
  • 753
  • 3
  • 9
  • 24
  • This is a good tutorial to implement searchable in android. http://javapapers.com/android/android-searchview-action-bar-tutorial/ Hope it helps! – Isaac Urbina Apr 07 '16 at 17:13

6 Answers6

6

After a long Research i have Analyse something

searchView.setSearchableInfo( searchManager.getSearchableInfo(new 
ComponentName(this,SearchResultsActivity.class)));

Here the activity.class is the name of the searchable activity where you want to pass the search query.

Vivek Samele
  • 340
  • 4
  • 8
  • 2
    Worked for me. The documentation is very unclear about this. https://developer.android.com/guide/topics/search/search-dialog Under the `configuring the search widget` section the comment says "Assumes current activity is the searchable activity" So it basically is saying when the SearchableActivity is not the current activity it should be `new ComponentName(this,SearchableActivity.class)` instead of `getComponentName()`. Very easy to miss – PrashanD Aug 18 '18 at 09:01
5

I had the same problem, and my search for an answer took me here, so here is what worked for me...

Make sure your searchables.xml file is in the correct location (i.e, in your res/xml/ folder) and that the same file does not contain any errors - otherwise you will find that (SearchManager)getSystemService(Context.SEARCH_SERVICE).getSearchableInfo(componentName) will return null, breaking your search functionality.

(Also, ensure you set componentName in the appropriate manner, because the example shown in the Android guide is for only when you are making searches and displaying searches in the same Activity.)

...am sharing in the hope it may save someone else a wasted 4 hours! :/

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
  • I have wasted three days, today is the fourth day. Can you look into my question [here](http://stackoverflow.com/questions/27567796/android-assisted-search-the-search-button-does-not-invoke-the-searchable-activi)? May be you can spot the problem? – Solace Dec 23 '14 at 09:24
  • lots of time wasted, finally, this helps, though I'm still to figure why is it null. – Raghvendra Singh Oct 06 '19 at 19:44
4

Solved the problem. The tag was needed to be for application .Removing it from activity and putting it under application did the trick.

Here is the latest application tag in manifest.xml

   <application
    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=".SearchResultsActivity"/>
    <activity
        android:name="com.punit.rateit.MainActivity"
        android:label="@string/app_name" >
        <meta-data android:name="android.app.default_searchable"           
            android:value="com.punit.rateit.SearchResultsActivity" /> 
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Puneet
  • 753
  • 3
  • 9
  • 24
  • 1
    This doesn't seem to work. There are two conflicting meta-data tags with the same name values. – John61590 Apr 16 '14 at 15:23
  • 2
    Check this one: http://stackoverflow.com/questions/11699206/cannot-get-searchview-in-actionbar-to-work U must delacre meta tag both in main activity (where you place the search widget and in the search activity) – Lạng Hoàng Oct 23 '14 at 05:09
  • 3
    Documentation tell different story.. To declare the searchable activity for an activity's search dialog, add a element inside the respective activity's element. The element must include the android:value attribute that specifies the searchable activity's class name and the android:name attribute with a value of "android.app.default_searchable". – Pankaj Mar 09 '15 at 07:29
  • 2
    I'm just baffled at how difficult this simple search thing is. I'm on my 3rd day trying to figure this out. – Chris Jeon Jan 17 '17 at 00:54
2

Creating a ComponentName object with the name of the Search Result activity class worked for me. Like this:

searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchStoreActivity.class)));
1

I was not using the strings within searchable.xml.

You MUST set it up that way or you will get null. I had just put in text instead of using @string.

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

I struggled with the problem for a day or two, it's not very clearly mentioned in the documentation.

searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

In the above the getComponentName function returns the name of the current component, which is good if your current component is handling/showing the search results, but if you have a different activity then you need to put the component name of that activity for search view to redirect searches to that activity.

searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultActivity.class)));

Also make sure to have no errors in searchable.xml file in res/xml folder.

Raghvendra Singh
  • 952
  • 9
  • 17