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

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>

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

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

//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);

Related

Intent is not triggered in android searchview

Here is my code for searchview in android.Search option is visible on the toolbar but when a text and entered and searched it does not trigger SearchActivity.
xml/searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/label"
android:hint="#string/search" >
</searchable>
menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.welcomecure.searchview">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<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=".SearchResultsActivity" />
</activity>
<activity
android:name=".SearchActivity"
android:label="#string/app_name">
<!-- to identify this activity as "searchable" -->
<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="#xml/searchable" />
</activity>
</application>
</manifest>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
SearchActivity.java
public class SearchActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search
}
}
}
Your Activity is named SearchActivity, but you declared SearchResultsActivity in manifest:
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
From the docs:
To declare the searchable activity for an activity's search dialog,
add a element inside the respective activity's
element. The element must include the android:value
attribute that specifies the searchable activity's class name and the
android:name attribute with a value of
"android.app.default_searchable".
try to use the same activity name from the calling activity, you used SearchActivity to show the search result, try to use the same activity name
<activity android:name=".MainActivity">
<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=".SearchActivity" />
</activity>
You can also do this programmatically.
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) { // this is triggered when you press the key search from keyboard.
return true;
}
#Override
public boolean onQueryTextChange(String newText) { // this is triggered when you change your search text.
return true;
}
});

Search on ActionBar

Level:Beginner
I am trying to implement a search bar on ActionBar. I want that when someone clicks the search icon, he should get a text field, where he simply fills in the text and can search by submitting through the keyboard search Button. Following several tutorials, I am quite confused.
When I do not add
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
I get an icon for search, but it does not expands into a text field. So I am not able to search anything from there.
when I add
`searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); `
I get an error 'unfortunately, Application has stopped' and the logcat shows:-
Process: com.example.bhavya.myapplication, PID: 23261
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setIconifiedByDefault(boolean)' on a null object reference
at com.example.bhavya.myapplication.MainActivity.onCreateOptionsMenu(MainActivity.java:27)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
I also tried to set 'intent-filter' and 'meta-data' in the following way after I was unsuccesful
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bhavya.myapplication" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
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" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".SearchableActivity"/>
</activity>
<activity
android:name=".SearchableActivity"
android:label="#string/title_activity_searchable" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
but nothing changed, I also made a folder "xml" and set searchable configuration
<?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/app_name" >
</searchable>
This is my menu.
<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=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
This is my 2nd activity
public class SearchableActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;I get an icon for search, but it does not expands into a text field. So I am not able to search anything from there.
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search
}
}
}
I tried to look up a lot, but only got confused more. Please Help.

Start new activity from SearchView

I have 2 activities: the first has a action bar with a search view, the second should display the results of the search query.
androidmanifest:
<activity
android:name=".SearchActivity"
...
android:launchMode="singleTop">
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
...
</activity>
<activity
android:name=".ResultsActivity"
...
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
searchable.xml
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/enter_a_word" />
SearchActivity
....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_noun_list, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()));
return true;
}
....
ResultsActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
}
...
}
the problem is that after a query is entered into the searchview, nothing happens. No errors, nothing. How can i open the resultsactivity after the query is entered in the searchactivity?
This answer is a little late but I feel it'll be useful for future viewers. The dilemma seems to come from the ambiguity of the Android SearchView tutorial. The scenario they cover assumes you will be displaying the results in the same Activity the SearchView resides. In such a scenario, the Activity tag in the AndroidManifest.xml file would look something like this:
<activity
android:name=".MainActivity"
android:label="#string/main_activity_label"
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>
Then, to handle the results in the same Activity, you would Override the onNewIntent method:
#Override
public void onNewIntent(Intent intent){
setIntent(intent);
if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//now you can display the results
}
}
However, in a situation where we want to display the results in another Activity, we must put the Intent Filter and meta tag into the results Activity and introduce a new meta tag for the SearchView Activity. So, our Activities will look something like this in the AndroidManifest.xml file:
<activity
android:name=".MainActivity"
android:label="#string/main_activity_label"
android:launchMode="singleTop">
<!-- meta tag points to the activity which displays the results -->
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
<activity
android:name=".SearchResultsActivity"
android:label="#string/results_activity_label"
android:parentActivityName="com.example.MainActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
<!-- meta tag and intent filter go into results activity -->
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
Then, in our MainActivity's onCreateOptionsMenu method, activate the SearchView (assumes you're adding the SearchView to the ActionBar). Rather than using getComponentName() in the SearchManager's getSearchableInfo() method call, we instantiate a new ComponentName object using the MainActivity's context and the SearchResultsActivity class:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
SearchView search = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
search.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
search.setQueryHint(getResources().getString(R.string.search_hint));
return true;
}
Finally, in our SearchResultsActivity class, in the onCreate method, we can handle the search results:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
}
}
Don't forget to create the searchable.xml resource file and add the SearchView to your layout.
searchable.xml (res/xml/searchable.xml; create xml folder under res if needed):
<?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/search_hint"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>
Layout (example of adding the SearchView to ActionBar as a menu item):
<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="com.example.MainActivity">
<group android:checkableBehavior="single">
<item android:id="#+id/action_search" android:title="Search"
android:orderInCategory="1" app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</group>
</menu>
Resources:
Display Results In Same Activity
Display Results In Different Activity
ComponentName
I understood the problem i had also faced the same, This is happening because you are passing the current component name by passing the
getComponentName()
This will be initialize by the current activity name so you need to initialize it with the searchable activity name in given below format and pass the same Component instance it starts the new activity.
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
search.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
search.setQueryHint(getResources().getString(R.string.search_hint));
Hope it I have answered the Question!
Without seeing your activity code, I would suggest you try this approach - also assuming you have all the files created as stated above;
In your results activity,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.core_actions, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setQueryHint(getString(R.string.search_hint));
searchView.setOnQueryTextListener(this);
return true;
}
Remember that this is the activity that has your data that you want to make searchable:
You must implement the SearchView.OnQueryTextListener interface in the same activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
ProductsResulstActivity.this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
productFilterAdapter.getFilter().filter(newText);
if (TextUtils.isEmpty(newText)) {
listView.clearTextFilter();
}
else {
listView.setFilterText(newText);
}
return true;
}
productFilterAdapter is the adapter that you must create beforehand.
It should implement Filterable interface. I hope this helps.
If you need further assistance, please let me know. Good luck

MainActivity relaunched onQueryTextSubmit

I've set the searchable config but now am I facing strange problem. While I submit the search query the SearchActivity.class is launched for a second and then goes back to MainActivity.class. I don't know, what happens.
See my codes:
in MainActivity:
#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);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
if (null != mSearchView )
{
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener()
{
public boolean onQueryTextChange(String newText)
{
return true;
}
public boolean onQueryTextSubmit(String query)
{
String said = query.toString();
Log.v(said, "submited");
Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
intent.setAction(Intent.ACTION_SEARCH);
intent.putExtra("query", query);
startActivity(intent);
return true;
}
};
mSearchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
AndroidManifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.animalist.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name="com.example.animalist.MoreActivity"
android:label="#string/title_activity_more"
android:launchMode="singleTop"
android:parentActivityName="com.example.animalist.MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.animalist.MainActivity" />
</activity>
<activity
android:name="com.example.animalist.SearchActivity"
android:label="#string/title_activity_search"
android:launchMode="singleTop"
android:parentActivityName="com.example.animalist.MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.animalist.MainActivity" />
</activity>
</application>
What am I doing wrong?
Based on this thread, it seems that it is a bug of Android.
Found this temporary "solution" from here (Japanese). It works for me.
#Override
public boolean onQueryTextSubmit(String query) {
mSearchView.clearFocus();
......
// your code to process the query
......
return false;
}

Cannot able to make SearchActivity to start

I try to make SearchView in action bar to start a SearchResultsActivity which is registered to handle ACTION_SEARCH. I do all required steps but still didn't work!
Here's the code:
1. Search View menu item: (menu/main.xml)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/search_action"
android:icon="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="always|collapseActionView"
/>
</menu>
2. Searchable configuration (xml/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:hint="#string/search_hint"/>
2.1 adding Searchable configuration link to MainActivity in AndroidManifest.xml:
<activity
android:name="com.me.searchonpre3.MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
3. Bind the Search View menu item with the search configuration (MainActivity.java):
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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
SearchView searchView = (SearchView) menu.findItem(R.id.search_action).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
}
return true;
}
4. add new Activity "SearchResultsActivity" and mark it to handle SEARCH_ACTIONs:
public class SearchResultsActivity extends ListActivity {
public static final String TAG = SearchResultsActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "In SearchResult OnCreate");
setContentView(R.layout.activity_search_results);
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
new String[]{"Product1", "Product2"}));
}
}
AndroidMainfest.xml:
<activity
android:name="com.me.searchonpre3.SearchResultsActivity"
android:label="#string/title_activity_search_results">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
</activity>
All the above settings and the SearchResultsActivity is not started when press the Search keyboard icon.
What else Should I do???
Note: When I add a setOnQueryTextListener, the callbacks being called on the MainActivity class.
It seems that Android tutorials are misleading.
The answer is here: https://stackoverflow.com/questions/11699206#11704661

Categories

Resources