javaandroidxmlsearchviewsearchactivity

Android Search view not invoking search activity


Please dont mark as duplicate as I have viewed several other same question and those solution didnt worked for me.

category_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/category.search"
        android:title="Search"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

Mainfest.xml

<activity android:name=".CategoryActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SearchResultsActivity"
            android:label="@string/title_activity_search_results"
            android:theme="@style/AppTheme.NoActionBar"
            android:launchMode="singleTop">
            <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" />
            <meta-data                android:name="android.support.PARENT_ACTIVITY"                android:value=".CategoryActivity" />
        </activity>

CategoryActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.category_menu, menu);
        SearchView searchView = (SearchView) menu.findItem(R.id.category_search).getActionView();
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
        return true;
}

SearchResultsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG,"Search Began");
    setContentView(R.layout.search_results_activity);
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    Log.d(TAG, "Herer");
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        //use the query to search your data somehow
        Log.d(TAG, query);
    }
}

Searchable.xml under res/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="Recipe Search" />

Solution

  • you should add onQueryTextListener in searchView.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.category_menu, menu);
            SearchView searchView = (SearchView) menu.findItem(R.id.category_search).getActionView();
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
            searchView..setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    startActivity(CategoryActivity.this, SearchResultActivity.class);
                    return true;
                }
    
                @Override
                public boolean onQueryTextChange(String newText) {
                    return false;
                }
            });
            return true;
    }