sidney
sidney

Reputation: 2714

Cannot pass a search variable in Android search widget

I cannot pass my search query which is sent from my MainActivity.java to my SearchResultsActivity.java, with a search widget as described on the official guide.

However, I don't want my search widget to be set on the actionbar.

I got no error. My only wish is to get my query displayed thanks to "Log.v". But the official guide seems only to explain how to call the another activity when we got a search widget inside the actionbar, which isn't our case here.

How to retrieve the variable query in my MainActivity.java ?



MainActivity.java:

package com.example.fidbacks_search;

import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.Menu;
import android.widget.SearchView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

}



SearchResultsActivity.java

package com.example.fidbacks_search;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class SearchResultsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);
         Log.v("QUERY", "this isn t even being displayed when the search button is pressed");

        // 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);
          Log.v("QUERY", query.toString());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.search_results, menu);
        return true;
    }

}



Searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="test"
    android:hint="test2" >
</searchable>



AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.fidbacks_search.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:name="com.example.fidbacks_search.SearchResultsActivity"
            android:label="@string/title_activity_search_results"
            android:launchMode="singleTop">
            <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>



layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <SearchView
        android:id="@+id/searchfield"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="144dp"
        android:iconifiedByDefault="false" >

    </SearchView>

</RelativeLayout>

cantpassvariable

Upvotes: 0

Views: 837

Answers (1)

gatlingxyz
gatlingxyz

Reputation: 753

I'm not sure exactly how you want it done, but I've used SearchView recently and to get what you want, I would simply do the following.

In your activity, add to onCreate something like:

searchView = (SearchView) findViewById(R.id.searchfield);

SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView.setQueryHint("Enter query here");
searchView.setOnQueryTextListener(this);

Then have your current activity implement SearchView.OnQueryTextListener's two methods; the one you are looking for is onQueryTextSubmit. Here, use the query that's passed to it and call your SearchResultsActivity.

Intent intent = new Intent(this, SearchResultsActivity.class);
intent.putExtra("query",query);
startActivity(intent);

Then have your SearchResultsActivity get the query in `onCreate':

query = getIntent().getStringExtra("query");

Then, use the query and do whatever you want with it.

Upvotes: 1

Related Questions