Search code examples
androidsearchview

SearchView not searching


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());}
    }
}

Solution

  • 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
            }
        }
    );