1

I am trying to implement search functionality in my android app using SearchView within the layout of my app. I purposefully chose to have it in the layout rather than in the Action Bar.

The problem is that once I type the search text and hit the enter/search key on the keyboard nothing happens. The searchable intent is never called. I feel like I must be missing something that is causing the search not to trigger

I apologize if I haven't included the code in the correct format below. Please let me know if there is any additional information that would help. Thanks in advance!

Manifest

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.lotr.arda.MainActivity"
        android:label="@string/app_name" >

    </activity>
    <activity
        android:name=".WebActivity"
        android:theme="@android:style/Theme.NoTitleBar" >
      </activity>
         <activity
        android:name=".menuActivity"
        android:theme="@android:style/Theme.NoTitleBar" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
        </activity>
         <activity android:name=".searchActivity" >
    <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/search

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

SearchActivity.java

package com.lotr.arda;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;

public class searchActivity extends ListActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_page);

    // Get the intent, verify the action and get the query
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      String query = intent.getStringExtra(SearchManager.QUERY);

      System.out.println(query);
      //doMySearch(query);
    }
}
}

menuActivity.java (layout where searchview is located)

package com.lotr.arda;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.app.Activity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SearchView;



public class menuActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_page);
    try{    

        AdView myad = (AdView) findViewById(R.id.ad);
         AdRequest adRequest = new AdRequest(); 

         myad.loadAd(adRequest);

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

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

         Button alpha = (Button)this.findViewById(R.id.button1);
         alpha.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                       Intent intent = new Intent(menuActivity.this, MainActivity.class);
                        startActivity(intent);                      
                }});            
    }catch(Exception e) {System.out.println(e.toString());}
    }
}
Chris2222000
  • 67
  • 3
  • 12
  • possible duplicate of [Cannot get searchview in actionbar to work](http://stackoverflow.com/questions/11699206/cannot-get-searchview-in-actionbar-to-work) – Clint Jul 03 '14 at 22:08

3 Answers3

3

You are missing a SearchView#setOnQueryTextListener. You'll need to override the OnQueryTextSubmit() method to actually perform and handle your search.

EDIT: I am assuming you're querying a database and using a Cursor for the results.

mSearchView.setOnQueryTextListener(new OnQueryTextListener()
    {
        @Override
        public void onQueryTextSubmit(String query) {
            Cursor c = getContentResolver.query(URI, null, "col_1 = ? OR col_2 = ?", new String[] { query, query }, null);
            //Do whatever you want with your Cursor here
        }
    }
);
Vinay S Shenoy
  • 4,028
  • 3
  • 31
  • 36
  • Thanks Vinay. Do you happen to have an example of that? I tried to added SearchView SV = (SearchView)findViewById(R.id.searchView1); SV.setOnQueryTextListener(new OnQueryTextListener() { to my java file but im not sure how to add the override you mentioned – Chris2222000 May 21 '13 at 20:06
  • 2
    The call to `setOnQueryTextListener()` is not necessary. The problem you have here seems to be the missing `default_searchable` setting under AndroidManifest. – Clint Jul 03 '14 at 21:47
1

For a quick look it seems you missed default_searchable setting in your AndroidManifest.

Under activity setting for MainActivity, you should add this

<meta-data
  android:name="android.app.default_searchable"
  android:value=".WebActivity" />
Clint
  • 1,014
  • 1
  • 10
  • 15
0

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.)

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253