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
Related
I have a SearchView implementation that's not working at all. I have tried a lot of things, but nothing is working.(this,this and other answers)
What am I doing wrong? I am trying to log the partial results or something that tells me it is working, but I don't get anything.
Manifest.xml
<activity
android:name=".MapaActivity"
android:label="#string/title_activity_mapa"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
</activity>
menu.xml
<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.buweb.bu.MapaActivity">
<item android:id="#+id/search"
android:title="#string/menu_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Activity
public class MapaActivity extends BaseActivity implements OnMapReadyCallback, SearchView.OnQueryTextListener {
... //A lot of code doing stuff with map.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_mapa, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
Log.d("", "query:" + query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Log.d("", "query:" + newText);
return false;
}
You are missing some implementations:
1-Create a Searchable Configuration:
A searchable configuration defines how the SearchView behaves and is defined in a res/xml/searchable.xml file.
<?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- In your application's manifest file, declare a <meta-data> element that points to the res/xml/searchable.xml file, so that your application knows where to find it. Declare the element in an <activity> that you want to display the SearchView in:
<activity ... >
...
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
3- In the onCreateOptionsMenu() method that you created before, associate the searchable configuration with the SearchView by calling setSearchableInfo(SearchableInfo):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_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;
}
For further information you can refer to the following links:
https://developer.android.com/training/search/setup.html#add-sv
https://developer.android.com/guide/topics/search/search-dialog.html#SearchableActivity
How to implement a Searchview in android?
How to use SearchView in Toolbar Android
Implementing SearchView in action bar
I'm trying to implement searching in my android app. There are two activities, the MainActivity where search is launched (via searchview in action bar) and ResultsActivity, where search is handled and a list is inflated with results. The problem is that the getSearchableInfo method that i invoke in onCreateOptionsMenu in the main activity returns a null, like no searchable.xml were attached to the activity. I'v tried many combinations of meta data arrangement in the manifest, also every solution found on the internet and other stackoverflow questions, but nothing seems to work.
These are the sources:
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
<activity
android:name="com.example.webparser.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>
<activity android:name="com.example.ResultsActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<meta-data
android:name="android.app.default_searchable"
android:value=".ResultsActivity" />
</application>
** 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);
SearchManager searchManager = (SearchManager) getSystemService(getApplicationContext().SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
Log.d("Search:", searchManager.getSearchableInfo(getComponentName())
.toString());
return true;
}
** ResultsActivity.java **
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
elementi = new ArrayList<String>();
for(Ricetta ricetta:ricette){
if( ricetta.getNome().toLowerCase().matches(query) ){
elementi.add(ricetta.getNome());
}
}
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
elementi);
lista.setAdapter(adapter);
Log.i("LOG:", "searching");
}
}
** searchable.xml **
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
/>
Please notice that I inserted a Log.d where the searchview is supposed to be initialized, and where is called getSearchableInfo, so at runtime a nullpointerexception is raised and the app doesn't launch, but if that Log.d is removed then everything but the search works (on pressing enter in the search box nothing happens). I think there is a problem with the manifest, but I can't figure out what it is. Does anyone have any clue?
I also tried to launch the main activity in singleTop pointing the searchable meta data tags to the main activity, but the same exception is raised.
I notice that you are using Menu to try an obtain your search view instead of the findViewById function
SearchView searchView = (SearchView) menu.findItem(R.id.search)
.getActionView()
Should be more like this
SearchView searchView = (SearchView) findViewById(R.id.search);
This would give you the SearchView you are looking for.
Faced the same problem. Following helped me:
This is my MainActivity
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_list_view_test, menu);
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()) );
return true;
Note: Make sure that you have imported SearchView from Support.v7.widget
(import android.support.v7.widget.SearchView;)
This is my Menu.xml for MainActivity
< item android:id="#+id/search"
android:title="Search"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
In my case I have similar set up just a little bit different. I have:
<meta-data android:name="android.app.default_searchable"
android:value=".ResultsActivity" />
in my Main Activity. And
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
and
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
should work.
in my ResultActivity.
And I create a component name in my MainActivity with:
ComponentName componentName = new ComponentName(context, ResultActivity.class);
Note: be careful with your item layout:
Use: app:actionViewClass="android.support.v7.widget.SearchView" if you have problem with android:actionViewClass="android.support.v7.widget.SearchView"
I'm using the SearchView widget in ActionbarSherlock as a follows:
File.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.MenuSearch).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setSubmitButtonEnabled(true);
return true;
}
File.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/MenuSearch" android:title="#string/Bs"
android:icon="#drawable/ic_action_search"
android:showAsAction="always|withText"
android:actionViewClass="com.actionbarsherlock.widget.SearchView" >
</item>
</menu>
In my application I get to show the search icon and select it unfolds the search box for the widget, but when I write any search I do not know how to interact with the widget SearchView to launch a new activity and show a series of results.
With the command searchView.setSubmitButtonEnabled(true); appears a icon similar to 'play' and I suppose it is for just that, but I do not know how to interact with it.
Can anyone help?
Official android documentation here describes how start new activity to handle search query and display results.
First you need to create (if you havent already) searchable configuration in res/xml/searchable.xml, like this:
<?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" />
Set this configuration for your activity, that contains SearchView in actionbar, like this (in manifest):
<activity ... >
...
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
So basically, you need your searchable activity (new activity that will start after submitting search query) to handle the android.intent.action.SEARCH action. Declare it in manifest like this:
<activity android:name=".SearchResultsActivity" ... >
...
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
...
</activity>
And your searchable activity should look like this:
public class SearchResultsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
...
handleIntent(getIntent());
}
#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 data somehow
}
}
...
}
I am trying to make my app consist a SINGLE activity. This activity should be able to create a search and also receive a search. Unfortunately, I am getting a "double" search bar in my SearchView when I click on the search button in the action bar. I mean that there is a search bar (dark-- SearchView) that appears for a second in the action bar, and then a second one (white) appears OVER the action bar. Any help? What am I doing wrong?
Sorry, this search thing is all new and confusing to me.
MainActivity (the only activity):
public class MainActivity extends ActionBarActivity {
Menu mMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//getSupportActionBar().setDisplayShowTitleEnabled(false);
setContentView(R.layout.activity_main);
handleIntent(getIntent());
}
#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 data somehow
}
}
#SuppressLint("NewApi")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mMenu = 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) {
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
default:
return false;
}
}
#SuppressLint("NewApi")
#Override
public boolean onSearchRequested() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
MenuItem mi = mMenu.findItem(R.id.search);
if(mi.isActionViewExpanded()){
mi.collapseActionView();
} else{
mi.expandActionView();
}
} else{
//onOptionsItemSelected(mMenu.findItem(R.id.search));
}
return super.onSearchRequested();
}
}
main.xml (the menu xml):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:com.brianco.andypedia="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/search"
android:title="#string/action_settings"
android:icon="#drawable/ic_launcher"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
com.brianco.andypedia:showAsAction="always|collapseActionView"
com.brianco.andypedia:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
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"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" />
in the manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar" >
<activity
android:name="com.brianco.andypedia.MainActivity"
android:label="#string/app_name" >
<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>
<meta-data android:name="android.app.default_searchable"
android:value=".MainActivity" />
From Setting Up the Search Interface in the Android Documentation:
In your searchable activity, handle the ACTION_SEARCH intent by
checking for it in your onCreate() method.
Note: If your searchable activity launches in single top mode
(android:launchMode="singleTop"), also handle the ACTION_SEARCH intent
in the onNewIntent() method. In single top mode, only one instance of
your activity is created and subsequent calls to start your activity
do not create a new activity on the stack. This launch mode is useful
so users can perform searches from the same activity without creating
a new activity instance every time.
Please try to add the following attribute to your <activity> in your manifest file:
android:launchMode="singleTop"
This will make the same activity to receive the search intent.
More info here: http://developer.android.com/guide/topics/manifest/activity-element.html
Also, you have <intent-filter> declared twice, you should merge it into one element.
Okay, the problem had to do with calling onSearchRequested() in onOptionsItemSelected(MenuItem item). That is redundant when I have a SearchView and should only be called on older platforms.
So, I created a separate menu item for devices under Honeycomb. It is removed at runtime for newer devices. The SearchView is removed at runtime for older devices.
See updated code below:
#SuppressLint("NewApi")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mMenu = 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) {
//remove old
menu.removeItem(R.id.search_old);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
} else{
//remove new
menu.removeItem(R.id.search);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search_old:
onSearchRequested();
return true;
default:
return false;
}
}
#SuppressLint("NewApi")
#Override
public boolean onSearchRequested() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
MenuItem mi = mMenu.findItem(R.id.search);
if(mi.isActionViewExpanded()){
mi.collapseActionView();
} else{
mi.expandActionView();
}
} else{
//onOptionsItemSelected(mMenu.findItem(R.id.search));
}
return super.onSearchRequested();
}
THANKS,
This has worked for me.
Manifest:
<activity
android:name=".Buscar"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_buscar"
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>
Activity:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
query = intent.getStringExtra(SearchManager.QUERY);
mysearch(query);
}
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