Skip to content Skip to sidebar Skip to footer

Android Pass Bundle With Search

Hello i am using android 3.0 search widget(not the search mode), the problem is i need to pass some parameters along with the search word. Android isn't calling any of these se

Solution 1:

pass data with bundle in Search Widget:

layout/main.xml :

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello" /><Buttonandroid:id="@+id/button"android:text="test"android:layout_width="fill_parent"android:layout_height="wrap_content" /></LinearLayout>

layout/searchable.xml :

<?xml version="1.0" encoding="utf-8"?><searchablexmlns:android="http://schemas.android.com/apk/res/android"android:label="@string/search_label"android:hint="@string/search_hint"android:searchMode="showSearchLabelAsBadge"android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"android:voiceLanguageModel="free_form"android:voicePromptText="@string/search_invoke"android:searchSuggestSelection=" ? "
/>

values/strings.xml :

<?xml version="1.0" encoding="utf-8"?><resources><stringname="hello">Hello World, SearchTest!</string><stringname="app_name">Searchtest</string><stringname="search_label">Search Test</string><stringname="search_hint">1234</string><stringname="search_invoke">234</string><stringname="search_query_results">544545</string></resources>

AndroidManifest.xml :

<applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name" ><activityandroid:label="@string/app_name"android:name=".SearchWidgetExampleTest" ><intent-filter ><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter><meta-dataandroid:name="android.app.default_searchable"android:value=".ResultActivty" /></activity><activityandroid:name=".ResultActivty"android:label="@string/search_query_results"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.SAMPLE_CODE" /></intent-filter><intent-filter><actionandroid:name="android.intent.action.SEARCH" /><categoryandroid:name="android.intent.category.DEFAULT" /></intent-filter><meta-dataandroid:name="android.app.searchable"android:resource="@layout/searchable" /></activity></application>

ResultActivty.java:

package org.imranandroid.TestSearchexmp;
import android.app.Activity;
import android.app.SearchManager;
import android.os.Bundle;
import android.widget.Toast;

publicclassResultActivtyextendsActivity{
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bundle bundled= getIntent().getBundleExtra(SearchManager.APP_DATA);
        Long ttdata=bundled.getLong("listino_id");
        Toast.makeText(this, ttdata.toString(), Toast.LENGTH_SHORT).show();
    }
}

SearchWidgetExampleTest.java :

package org.imranandroid.TestSearchexmp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

publicclassSearchWidgetExampleTestextendsActivity {
    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Buttonbutton1= (Button) findViewById(R.id.button);
        button1.setOnClickListener(newOnClickListener() {
            @OverridepublicvoidonClick(View v) {
                onSearchRequested();
            }
        });
    }
    @OverridepublicbooleanonSearchRequested() {
        BundleappDataBundle=newBundle();
        appDataBundle.putLong("listino_id", 4434724724L);
        startSearch("imran", false, appDataBundle, false);
        returntrue;
    }
}

Happly Coding!!!

Solution 2:

when querying by searchview ( searchable || searchview widget ) you can pass parameter to searchActivity ( searchResultActivity ) by define your own OnQueryTextListener like this.

searchView.setOnQueryTextListener(newOnQueryTextListener() {

        @OverridepublicbooleanonQueryTextChange(String arg0) {
            // TODO Auto-generated method stubreturnfalse;
        }

        @OverridepublicbooleanonQueryTextSubmit(String query) {
            Intent searchIntent = newIntent(getApplicationContext(), SearchResultsActivity.class);
            searchIntent.putExtra(SearchManager.QUERY, query);
            searchIntent.setAction(Intent.ACTION_SEARCH);
            startActivity(searchIntent);
            returntrue;
        }
    });

Solution 3:

Check if you have hard coded strings for android:hint or android:label in res/xml/searchable.xml. They must be string resource ids.

Solution 4:

If your calls is syncronic(you perform one search request and waiting for response and don't perform any search request until you got your response) then it's easy.

Just create a helper static class to do it for you.

Some thing like this:

publicstatic Bundle myStaticClass;
.
.
.
myStaticClass.putString(key, value);
callSearch();
.
.
.
afterSearchResponse();
String value = myStaticClass.get(key);

Solution 5:

you can use the follow

in Menu

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_al_ayats, menu);

    SearchManagersearchManager=
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchViewsearchView=
            (SearchView) menu.findItem(R.id.action_search).getActionView();
    BundleappData=newBundle();
    appData.putString("hello", "world");

    searchView.setAppSearchData(appData);

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

    returntrue;
}

and in your search result activity put the below

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_search_result);

    handleIntent(getIntent());
}

    privatevoidhandleIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String extrastring=intent.getBundleExtra(SearchManager.APP_DATA).getString("hello");
        //use the query to search your data somehowgetSupportActionBar().setTitle(extrastring);
    }
}

Post a Comment for "Android Pass Bundle With Search"