Cannot search in the action bar.

I am new to java and android development; and I worked on the app in witch. I want an action bar with SearchView . I looked at Google examples, but I can't get it to work. I have to do something wrong, and I'm going to abandon application development: D I searched, but I did not find anything useful. Maybe you guys can help me :)

Problem : I open the search window as it should, but after that nothing happens at all, I noticed that my searchManager.getSearchableInfo (getComponentName ()) returns null. Also, my hint that I gave does not appear in my search box, the witch makes me think that maybe the application cannot find my searchable.xml? Just talk :)

MainActivity.java

 public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().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())); return true; } } 

searchable.xml

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

Androidmanifest

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.searchapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".SearchableActivity" > <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> </activity> <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> </application> </manifest> 

SearchableActivity.java

 package com.example.searchapp; import android.app.ListActivity; import android.app.SearchManager; import android.content.Intent; import android.os.Bundle; import android.util.Log; public class SearchableActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search); // 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); } } private void doMySearch(String query) { Log.d("Event",query); } } 

And here is the link to the mentioned project, I would be eternally grateful if someone would help me with this!

Source

+62
android android-actionbar searchview
Jul 28 '12 at 7:35
source share
8 answers

Your problem is in your AndroidManifest. For me it was almost the same as for you, because this is what I get after the documentation. But this is unclear or erroneous.

Watching the source of the Demos API, I found that the metadata “android.app.searchable” should go in your “results”, and in the main action (or in the activity where you host the SearchView) you point to another one with “android.app .default_searchable ".

You can see this in the following file, which is the manifest from my test project:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <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> <activity android:name=".SearchActivity" android:label="@string/app_name" > <!-- This intent-filter identifies this activity as "searchable" --> <intent-filter> <action android:name="android.intent.action.SEARCH" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <!-- This metadata entry provides further configuration details for searches --> <!-- that are handled by this activity. --> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity> </application> 

It is also important that the tooltip and label in the searchable.xml file are links, not hard-coded strings.

Hope it solves your problem. It took me a whole day to figure this out :(

+144
Jul 28 2018-12-12T00:
source share

Android manuals explicitly state:

 // Assumes current activity is the searchable activity searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 

What is NOT TRUE in your case. Because you do not want to search in MainActivity, but in SearchableActivity. This means that you should use this ComponentName instead of the getComponentName () method:

 ComponentName cn = new ComponentName(this, SearchableActivity.class); searchView.setSearchableInfo(searchManager.getSearchableInfo(cn)); 
+76
Aug 26 '14 at 6:51
source share

It also seems that the attributes android: hint and android: label MUST refer to lines in the strings.xml file. Being lazy and hard-coding string values ​​doesn't work at all :)

i.e. Do not execute:

 <?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="Search for something..." android:label="My App"> </searchable> 

But definitely do it (as the OP did right):

 <?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="@string/search_hint" android:label="@string/app_label"> </searchable> 
+13
Mar 31 '13 at 23:45
source share

If you are using Android Studio,

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

part may not work.

write the full name of the package, for example

 <meta-data android:name="android.app.default_searchable" android:value="com.example.test.SearchActivity" /> 
+5
May 2 '14 at 14:35
source share

be careful with "xml / searchable.xml". If you made HARD CODE with android:hint and android:label . This will not work. haha Must be:

 <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="@string/app_name" android:label="@string/search_lb" android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/> 
+5
Jul 08 '15 at 7:46
source share

In fact, there is no need to use the “missing part” as described in the answers above. This method (using SearchView ) works exactly as official docs say before using the search widget . Here a small change needs to be made as follows:

Properties in SearchableInfo are used to display labels, tips, suggestions, create intentions to launch screens of search results, and control other features, such as a voice button ( documents ).

Thus, the SearchableInfo object that we pass to SearchView.setSearchableInfo(SearchableInfo s) must contain the correct links to the target activity that must be displayed to display the search results. This is done by passing the ComponentName ComponentName our target activity. One constructor to create a ComponentName is

 public ComponentName (String pkg, String cls) 

pkg and cls are package names and class names of the target activity that will be launched to display the search results. Note:

pkg should point to the name of the root package of your project and

cls must be the fully qualified name of the class (i.e. the whole object)

eg:.

 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); // Get the SearchView and set the searchable configuration String pkg = "com.example.musicapp" String cls = "com.example.musicapp.search.SearchActivity" ComponentName mycomponent = new ComponentName(pkg,cls); SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(mycomponent)); searchView.setIconifiedByDefault(false); //if using on actionbar return true; } 

Take a look at other ComponentName constructors depending on your requirements.

+2
Dec 12 '13 at 10:26
source share

Make sure that your searchables.xml file is in the right place (i.e. in your res/xml/ folder) and that the same file does not contain errors - otherwise you will find that (SearchManager)getSystemService(Context.SEARCH_SERVICE).getSearchableInfo(componentName) will return null, violating your search function.

(Also, make sure you set componentName correctly, because the example shown in the Android manual only runs when you search and show searches in the same Activity.)

+1
Sep 30 '14 at 14:50
source share

Double check to see if a matching entry (e.g. search_hint and app_label) is present in /strings.xml

eg,

  <string name="app_label">FunApp</string> <string name="search_hint">Caps Off</string> 
-one
Dec 11 '14 at 2:47
source share



All Articles