Search activity does not start when you press enter

Search activity does not start when you press enter. Search for a search is well displayed in the action bar. But when I type a search query and press enter

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.punit.rateit" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.punit.rateit.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:theme="@style/Theme.AppCompat.Light" android:name=".SearchPageActivity"> <meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity" /> <intent-filter> <action android:name="android.intent.action.SearchPage" /> </intent-filter> </activity> <activity android:name="com.punit.rateit.SearchResultsActivity" android:launchMode="singleTop" > <intent-filter> <action android:name="android.intent.action.SEARCH" /> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> </intent-filter> </activity> </application> </manifest> 

Here is Menu.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:com.punit.rateit="http://schemas.android.com/apk/res-auto" > <!-- Search, should appear as action button --> <item android:id="@+id/search" android:icon="@drawable/ic_search" android:title="@string/action_search" com.punit.rateit:actionViewClass="android.widget.SearchView" com.punit.rateit:showAsAction="ifRoom" /> </menu> 

An activity that displays an action bar.

  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())); searchView.setIconifiedByDefault(false); searchView.setSubmitButtonEnabled (true); return super.onCreateOptionsMenu(menu); } 

SearchResult activity, which should be active when the submit search button is called,

 public class SearchResultsActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d("search", "search triggered"); setContentView(R.layout.searchpage); handleIntent(getIntent()); } @Override protected void onNewIntent(Intent intent) { Log.d("search", "search triggered"); setIntent(intent); handleIntent(intent); } private void handleIntent(Intent intent) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); Log.d("search", query); } } @Override public boolean onSearchRequested() { Log.d("search", "search triggered"); return false; // don't go ahead and show the search box } 
+9
android searchview
source share
7 answers

Solved a problem. The tag was needed to apply. Correction from activity and its inclusion in the application did the trick.

Here is the last application tag in manifest.xml

  <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity"/> <activity android:name="com.punit.rateit.MainActivity" android:label="@string/app_name" > <meta-data android:name="android.app.default_searchable" android:value="com.punit.rateit.SearchResultsActivity" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 
+4
source share

I had the same problem, and my search for an answer brought me here, and this is what worked for me ...

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 any errors - otherwise you will find that (SearchManager)getSystemService(Context.SEARCH_SERVICE).getSearchableInfo(componentName) will return null, violating your search functions.

(Also, make sure you set componentName appropriately, because the example shown in the Android manual is only for when you search and search in the same Activity.)

... I share the hope that he can save someone else in vain for 4 hours!: /

+5
source share

After much research, I have something to analyze

 searchView.setSearchableInfo( searchManager.getSearchableInfo(new ComponentName(this,SearchResultsActivity.class))); 

Here, action.class is the name of the search activity to which you want to submit the search request.

+5
source share

Creating a ComponentName object with the name of the action class "Search Result" worked for me. Like this:

 searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchStoreActivity.class))); 
+2
source share

This is a good tutorial for implementing search in android. http://javapapers.com/android/android-searchview-action-bar-tutorial/ Hope this helps!

+1
source share

I did not use the lines in the searchable.xml file.

You MUST configure it this way, otherwise you will get zero. I just entered the text instead of using @string.

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

I struggled with the problem for a day or two, it was not very clearly mentioned in the documentation.

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

The getComponentName function returns the name of the current component, which is good if your current component is processing / displaying the search results, but if you have a different activity, then you need to put the component name of this activity to represent the search in order to redirect the search to this action.

 searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultActivity.class))); 

Also make sure that there are no errors in the searchable.xml file in the res/xml folder.

0
source share

All Articles