4

I've got a SearchView that doesn't seem to be working. The SearchView shows in the menu bar, and I can enter text. However when I click the search icon on my phone's on-screen keyboard, nothing at all happens. Ideally, this would start a new activity, sending along a query string to be used. I tried to implement the SearchView.setOnSearchClickListener method to handle the click, but when I do that, the result activity loads as soon as I click the search icon to enter text, with no parameters. Any help at all pinpointing what I might be doing wrong would be greatly appreciated.

I create the menu like so in my Activity:

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.convert:
            ...
            return true;
        default:
           return super.onOptionsItemSelected(item);
    }
}

And the xml for the search:

<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"
            android:icon="@android:drawable/ic_menu_search"
            app:showAsAction="always"
            android:actionViewClass="android.support.v7.widget.SearchView"
            app:actionViewClass="android.support.v7.widget.SearchView"/>

        <item android:id="@+id/convert"
            android:title="@string/dlconvert" />
    </menu>

I've also added this to the manifest in the activity where the search shows:

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

And this is the definition of the search result activity:

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

        <intent-filter>
            <action android:name="android.intent.action.SEARCH"/>
        </intent-filter>
    </activity>

The searchable.xml file just contains:

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

EDIT - the problem is not the same as the possible duplicate. The activity that is supposed to receive the intent never starts at all.

halfer
  • 19,824
  • 17
  • 99
  • 186
chaoskreator
  • 889
  • 1
  • 17
  • 39

6 Answers6

2

Try

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_menu, menu);

        MenuItem item = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        searchView.setOnQueryTextListener(this);

        SearchView.SearchAutoComplete searchAutoComplete = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
        searchAutoComplete.setHintTextColor(Color.GRAY);
        searchAutoComplete.setTextColor(Color.BLACK);


        return true;

    }

than implements SearchView.OnQueryTextListener to your activity ans Override methods

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        Intent intent = new Intent(Activity.this, SearchActivity.class);
        intent.putExtra("query", query);
        startActivity(intent);
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }
}); 
Goku
  • 9,102
  • 8
  • 50
  • 81
  • this answer is the solution when search result activity is your main activity and you do not want another activity to show results. even android documents did not cover this. thanks @Goku – Reza Aug 06 '20 at 16:05
1

Even after implementing the onQueryTextSubmit, the problem continued. After a day of searching for the issue, I finally realized I had the <action android:name="android.intent.action.SEARCH"/> intent filter in the incorrect activity definition in the manifest file.

chaoskreator
  • 889
  • 1
  • 17
  • 39
0

In your onCreateOptionsMenu():

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        Intent intent = new Intent(MainActivity.this, SearchActivity.class);
        intent.putExtra("searchTerm", query);
        startActivity(intent);
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }
});
Saurabh Thorat
  • 18,131
  • 5
  • 53
  • 70
0

make sure you implement setOnQueryTextListener on your search view

And your desired operation in onQueryTextSubmit() method

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setOnQueryTextListener(new OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            //Log.e("onQueryTextChange", "called");
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            // start acitivy here
            return false;
        }

    });
    return true;

}

devgun
  • 1,003
  • 13
  • 33
0

In the activity where the SearchView is displayed you need to change

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

to

<meta-data
        android:name="android.app.default_searchable"
        android:value="ActivitySearchResults" />
Petterson
  • 831
  • 9
  • 21
-1

You will have to make "searchResultActivity" searchable. Add following code in your activity (which has searchview) in the manifest file.

<meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchResultActivity" /> 
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197