Search bar widget does not start search activity

I am trying to implement a search in my Android application using the search bar widget in the action bar.

I follow

http://developer.android.com/guide/topics/search/search-dialog.html http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

to do this.

I have two actions related to the search. SearchBar activity has an action bar, and AcceptSearch is my searchable activity. Despite the fact that I announced which action is a search activity, the request is not sent to AcceptSearch, and the action does not start.

I have configured the search bar so that the search bar hint appears when the widget is empty, but the hint never appears. Here is my code.

SearchBar

public class SearchBar extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.search_bar); } @Override 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(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setIconifiedByDefault(false); return true; } 

AcceptSearch

 public class AcceptSearch extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash_screen); //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); //Start the search doMySearch(); } } 

Searchable.xml

 <?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_label" android:hint="@string/search_hint"> </searchable> 

options_menu

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:title="Help" android:id="@+id/menu_help" android:showAsAction="always" /> <item android:title="Categories" android:id="@+id/menu_cats" android:showAsAction="always" /> <item android:id="@+id/menu_search" android:title="Search with Searchlet" android:icon="@drawable/ic_action_search" android:showAsAction="ifRoom|collapseActionView" android:actionViewClass="android.widget.SearchView"/> </menu> 

manifest

  <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.searchlet" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".SplashScreenActivity" android:label="@string/title_activity_splash_screen" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".AcceptSearch"> <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=".SearchBar" android:label="@string/app_name"> <intent-filter> <action android:name="com.example.searchlet.CLEARSCREEN" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest> 

I believe that all the necessary information recreates the situation.

Thanks to everyone in advance. I understand.

+6
java android android-manifest
source share
7 answers

I had a similar problem. I followed the grokking tutorial and this activity never started . I have tried Indrek I solutions that work for me.

You must be sure that you have the correct metadata under the correct parent. Metadata

 <meta-data android:name="android.app.default_searchable" android:value=".NameSearchActivity" /> 

must be under the application. The following metadata

 <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> 

should be under the search activity parameter inside AndroidManifest.xml.

In search activity, I did what he said in a tutorial previously linked to manage the action stack. I hope this solution will work for everyone with the same problem.

+7
source share

The textbooks apparently lack an important part. You must add <meta-data android:name="android.app.default_searchable" android:value=".MySearchActivityName" /> inside the <application> tags.

+18
source share

Pau Arlandis Martinez, thanks. Your solution works fine, and an active stack tutorial is useful. I entered this metadata in the <application> section:

 <meta-data android:name="android.app.default_searchable" android:value=".NameSearchActivity" /> 

And I noticed that the @Override onQueryTextSubmit method must return false in order to start the search operation. Otherwise, searchButton on the virtual keyboard does not work.

+2
source share

You do not have enough metadata in the ".SearchBar" section of the activity of the manifest file, which indicates the ".AcceptSearch" activity.

Add the following metadata to the .SearchBar activity:

 <!-- search widget target activity--> <meta-data android:name="android.app.default_searchable" android:value=".AcceptSearch" /> 

The code for the .SearchBar activity manifest has been updated here.

 <activity android:name=".SearchBar" android:label="@string/app_name"> <!-- search widget target activity--> <meta-data android:name="android.app.default_searchable" android:value=".AcceptSearch" /> <intent-filter> <action android:name="com.example.searchlet.CLEARSCREEN" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> 
+1
source share

For everyone who still has this problem, I also ran into this problem, if the Searchable action extends AppCompatActivity, the action does not start, I changed it to extend the action instead, and it worked.

+1
source share

The tooltip and label in the searchable.xml file are links, not hard-coded strings.

  <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="@string/search_hint" android:label="@string/app_name"/> 

This is how the activity containing the SearchView widget should be declared in the manifest

 <activity android:name="ACTIVITY PATH WHICH CONTAINS SEARCHVIEW WIDGET"> <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> 

Manifest Search Announcement.

  <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> 
0
source share

I had problems with the fact that SearchView did not start SearchActivity , it turned out to be a problem with the package / directory.

If the SearchableActivity not in the same directory as the Activity where your SearchView is located, you need to change:

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

to

 searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchableActivity.class))); 
0
source share

All Articles