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
searchManager.setOnDismissListener( new OnDismissListener() {
@Override
public void onDismiss() {
// TODO Auto-generated method stub
Log.v(MyApp.TAG, "on dismiss search");
}
});
searchManager.setOnCancelListener( new OnCancelListener() {
@Override
public void onCancel() {
// TODO Auto-generated method stub
Log.v(MyApp.TAG, "on dismiss cancel");
}
});
@Override
public boolean onSearchRequested() {
Bundle appData = new Bundle();
appData.putLong("listino_id", this.listino_id);
this.startSearch(null, true, appData, false);
return true;
// return super.onSearchRequested();
}
I need my activity to NOT be singleOnTop, here is my AndroidManifest.xml
<activity android:name=".activity.ListinoProdottiActivity" android:windowSoftInputMode="stateHidden">
<!-- Receives the search request. -->
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<!-- No category needed, because the Intent will specify this class component-->
</intent-filter>
<!-- enable the base activity to send searches to itself -->
<meta-data android:name="android.app.default_searchable"
android:value=".activity.ListinoProdottiActivity" />
<!-- Points to searchable meta data. -->
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
Here is my searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
/>
Android Bundles are generally used for passing data from one activity to another. Basically here concept of key-value pair is used where the data that one wants to pass is the value of the map, which can be later retrieved by using the key.
Passing data from one Activity to Activity in android The data can be passed to other activity using intent putExtra() method. Data is passed as extras and are key/value pairs . The key is always a String. As value you can use the primitive data types int, float, chars, etc.
Using putExtra() We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.
pass data with bundle in Search Widget:
layout/main.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android: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"?>
<searchable xmlns: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>
<string name="hello">Hello World, SearchTest!</string>
<string name="app_name">Searchtest</string>
<string name="search_label">Search Test</string>
<string name="search_hint">1234</string>
<string name="search_invoke">234</string>
<string name="search_query_results">544545</string>
</resources>
AndroidManifest.xml :
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".SearchWidgetExampleTest" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".ResultActivty" />
</activity>
<activity android:name=".ResultActivty"
android:label="@string/search_query_results">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android: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;
public class ResultActivty extends Activity{
@Override
protected void onCreate(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;
public class SearchWidgetExampleTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onSearchRequested();
}
});
}
@Override
public boolean onSearchRequested() {
Bundle appDataBundle = new Bundle();
appDataBundle.putLong("listino_id", 4434724724L);
startSearch("imran", false, appDataBundle, false);
return true;
}
}
Happly Coding!!!
when querying by searchview ( searchable || searchview widget ) you can pass parameter to searchActivity ( searchResultActivity ) by define your own OnQueryTextListener like this.
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
Intent searchIntent = new Intent(getApplicationContext(), SearchResultsActivity.class);
searchIntent.putExtra(SearchManager.QUERY, query);
searchIntent.setAction(Intent.ACTION_SEARCH);
startActivity(searchIntent);
return true;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With