Andres Valencia
Andres Valencia

Reputation: 483

How to open a new activity to show results from a SearchView?

I've been working in an application that uses a SearchView Widget as an ActionView in the ActionBar.

The problem occurs when I type a search and hit the search button, it opens the same activity, what I want to do is to open a new Activity and show the results on a ListView, how can this be fixed?

This is my AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="andres.hotelsoria" >

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher_hotel"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.SEARCH" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
    <activity
        android:name=".SearchableActivity"
        android:label="@string/title_activity_searchable" >
    </activity>
</application>

Upvotes: 1

Views: 4254

Answers (3)

rogerwar
rogerwar

Reputation: 338

read this post http://developer.android.com/guide/topics/search/index.html

as mentioned in this document follow these steps

1)Create a folder in res->xml->searchable.xml paste content as mention in documentation

2)Go to AndroidManifest.xml and change Activity(where you want to deliver result for search) to this

<activity android:name=".SearchableActivity" >
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
               android:resource="@xml/searchable"/>
</activity>

3)Declare the SearchView in menu.xml file as

<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="your activity context" >

<item
    android:id="@+id/mi_search"
    android:title="@string/search"
    android:orderInCategory="2"
    android:icon="@drawable/searchicon"
    app:showAsAction="collapseActionView|ifRoom"
    app:actionViewClass="android.support.v7.widget.SearchView" />

4)in onCreateOptionsMenu(Menu menu) do this code

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);

// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; 

5)in onOptionsItemSelected. ->fetch Id of search view ->simple paste onSearchRequested() inside block

public boolean onSearchRequested() {
return super.onSearchRequested();

}

6)register searchView with onQueryTextListener and do what you want to do http://developer.android.com/reference/android/widget/SearchView.OnQueryTextListener.html

Upvotes: 1

Manos
Manos

Reputation: 1501

You can start a new activity by attaching a OnQueryTextListener to the SearchView.

    final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    Intent intent = new Intent(getApplicationContext(), SearchableActivity.Class);
                    startActivity(intent);
                    return true;
                }

                @Override
                public boolean onQueryTextChange(String newText) {

                    return true;

                }
            };
searchView.setOnQueryTextListener(queryTextListener);

Upvotes: 4

Surender Kumar
Surender Kumar

Reputation: 1123

//You hav to start the new activity like this SearchView.OnQueryTextListener textListener = new SearchView.OnQueryTextListener() {

    @Override
    public boolean onQueryTextSubmit(String query) {
        //use intent here to start new activity and pass "query" string. 
        return true;
    }

    @Override
    public boolean onQueryTextChange(String newText) {

        return true;

    }
};

searchView.setOnQueryTextListener(textListener);

Upvotes: 0

Related Questions