Intent is not triggered in android searchview

Here is my code for searchview in android.Search option is visible on the toolbar but when a text and entered and searched it does not trigger SearchActivity.

xml/searchable.xml

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

menu/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/search"
    android:title="@string/search_title"
    android:icon="@drawable/ic_search"
    app:showAsAction="collapseActionView|ifRoom"
    app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.welcomecure.searchview">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
          <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=".SearchResultsActivity" />
        </activity>

        <activity
          android:name=".SearchActivity"
          android:label="@string/app_name">

          <!-- 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>
  </application>

</manifest>

MainActivity.java

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    // Inflate menu to add items to action bar if it is present.
    inflater.inflate(R.menu.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;
 }
}

SearchActivity.java

public class SearchActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    handleIntent(getIntent());
}

@Override
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()));

    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
    }
 }
}

Answers


Your Activity is named SearchActivity, but you declared SearchResultsActivity in manifest:

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

From the docs:

To declare the searchable activity for an activity's search dialog, add a element inside the respective activity's element. The element must include the android:value attribute that specifies the searchable activity's class name and the android:name attribute with a value of "android.app.default_searchable".


try to use the same activity name from the calling activity, you used SearchActivity to show the search result, try to use the same activity name

<activity android:name=".MainActivity">
          <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>

You can also do this programmatically.

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) { // this is triggered when you press the key search from keyboard.
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) { // this is triggered when you change your search text. 
            return true;
        }
    });

Need Your Help

Thor equivalent or similar scripting framework for Javascript?

javascript ruby bash thor

Is there a Thor equivalent (ruby tool) or similar scripting framework for Javascript that could be used to build scripts?