Even after reading the samples and a few questions on SO, I still dont figure out why my searchwidget does nothing ... !
Manifest.xml simplified:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".ResultActivity" >
<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=".MainActivity"
android:label="#string/title_activity_main" >
</activity>
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" >
</searchable>
menu/activity_main.xml (searchwidget in action bar..) :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item android:id="#+id/menu_search"
android:title="#string/menu_search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView" />
MainActivity where the search is supposed to happen:
public class MainActivity extends Activity implements OnClickListener {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, 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();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
searchView.setSubmitButtonEnabled(true);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
// app icon in action bar clicked; go parameters
Intent intent = new Intent(this, ParametersActivity.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
case R.id.menu_search:
onSearchRequested();
return true;
}
return true;
}
}
and Resultactivity :
public class ResultActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search_result);
// 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);
doMySearch(query);
Toast.makeText(this, "QUERY : " + query, Toast.LENGTH_SHORT).show();
}
}
I've followed API guide, and I dont see where is my mistake, if anyone can help, I'd appreciate !!
Thanks.
Nico.
Related
I am trying to implement WhatsApp like Search in my application.I have to implement the following screen :
As you can see in the screenshot,I have a search icon as option 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/action_search"
android:icon="#drawable/search"
android:orderInCategory="100"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView" />
</menu>
On clicking search icon search view will be opened .
CODE:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_friend_list_activity, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(getApplicationContext(), "Search button clicked", Toast.LENGTH_SHORT).show();
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) FriendsListActivity.this.getSystemService(Context.SEARCH_SERVICE);
if (item != null) {
searchView = (SearchView) item.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(FriendsListActivity.this.getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Log.e("Query",query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.xml
<activity
android:name=".activity.FriendsListActivity"
android:parentActivityName=".activity.WelcomeActivity">
<!-- To display the search view-->
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity android:name=".activity.SearchResultsActivity">
<intent-filter>
<action
android:name="android.intent.action.SEARCH"
android:launchMode="singleTop" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
You can see here i have searchable activity named SearchResultsActivity.
I am following this tutorial.
SearchResultsACtivity:
public class SearchResultsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
Log.e("onCreate","in SerachActivity called");
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);
Log.e("Query1",query);
//use the query to search your data somehow
}
}
}
I think i am doing something wrong here.When i am trying to search ,SearchResultsActivity is not called .Please help me how can i implement search as WhatsApp do?
I guess you're reading the android tutorial here, that page is very confusing if not completely wrong. There're two places went wrong in your code.
When you call searchView#setSearchableInfo, you're telling Android what activity to launch to handle search result, in your case SearchResultsActivity. So you can't set component name to FriendsListActivity, instead change it to SearchResultsActivity.searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)))
In order for an activity to become a searchable that's able to be found by searchManager, you need to add the android.app.searchable meta-data label to the Activity block in your manifest. Note this label should NOT be added to the activity you display the SearchView, the tutorial page totally states it wrong. So change your SearchResultsActivity to the following.
If you made these two changes, your SearchView from FriendsListActivity would correctly holds a mSearchable instance that's able to start SearchResultsAcitivity. Otherwise it will be null.
<activity android:name=".activity.SearchResultsActivity">
<intent-filter>
<action
android:name="android.intent.action.SEARCH"
android:launchMode="singleTop" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"/>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
You have wrote like this: (so you passed FriendsListActivity instead of SearchResult Activity):
searchView.setSearchableInfo(searchManager.getSearchableInfo(FriendsListActivity.this.getComponentName()));
but you should pass the componentName like this:
ComponentName componentName = new ComponentName(FriendsListActivity.this , SearchResultsActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
i am working on action bar . i want to add searchview option on the action bar but app crash here.
this is crash
this is my code of option menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/search"
android:title="Search"
android:icon="#drawable/search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"/>
this is the code of searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/enter"
android:includeInGlobalSearch="false"
android:label="#string/search"
android:searchSettingsDescription="#string/search_global_description" />
and this is my activity code
public class Thrd extends ActionBarActivity {
Menu m;
final Context context=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thrd);
getSupportActionBar().setTitle("3rd page");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#20a780"));
getSupportActionBar().setBackgroundDrawable(colorDrawable);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate( R.menu.options_menu, menu );
// Add SearchWidget.
SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
SearchView searchView = (SearchView) menu.findItem( R.id.search ).getActionView();
searchView.setSearchableInfo( searchManager.getSearchableInfo( getComponentName() ) );
return super.onCreateOptionsMenu( menu );
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
case R.id.action_Exit:
openExit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openExit() {
}
}
please help me to solve my problem.thanks advance
This was how I implemented my search handling.
In the XML folder under layout, add a searchable.xml file and put this code like so:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="Search Trends" />
Then in your ANDROID manifest file, add
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.tobisoft.trendify.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
//This is the meta to add for your activity
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
Also you would need to make an activity for the search results like so:
<activity
android:name=".SearchResultsActivity"
android:label="#string/app_name"
android:theme="#style/MyMaterialTheme">
<!-- 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>
now in the menu folder, with a file name main.xml (or anything you are using)
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:orderInCategory="100"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
You can get the icon yourself by a simple google search. You would also need to add the AppCompat Library to your project, something you can also do from a simple google search. In the SearchResultsActivity, add this:
public class SearchResultsActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.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;
}
#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
}
}
}
The activity_result.xml layout file is this :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns: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:background="#ff141414">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="#dimen/_25sdp"
android:textStyle="bold"
android:text="Search Result goes here!"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
In your MainActivity.java add this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.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;
}
All this shld help your app on the right track. Hope it helps
Changing to import android.support.v7.widget.SearchView helped me and also change these lines
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView= (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);`
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.
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);
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);
}