Alejandro Cumpa
Alejandro Cumpa

Reputation: 2353

SearchView not working

I have a SearchView implementation that's not working at all. I have tried a lot of things, but nothing is working.(this,this and other answers) What am I doing wrong? I am trying to log the partial results or something that tells me it is working, but I don't get anything.

Manifest.xml

  <activity
            android:name=".MapaActivity"
            android:label="@string/title_activity_mapa"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH"/>
            </intent-filter>
        </activity>

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.buweb.bu.MapaActivity">
    <item android:id="@+id/search"
        android:title="@string/menu_search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

Activity

public class MapaActivity extends BaseActivity implements OnMapReadyCallback, SearchView.OnQueryTextListener { 

 ... //A lot of code doing stuff with map.



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

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

        return true;
    }


    @Override
    public boolean onQueryTextSubmit(String query) {
        Log.d("", "query:" + query);
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        Log.d("", "query:" + newText);
        return false;
    }

Upvotes: 1

Views: 4413

Answers (1)

Darush
Darush

Reputation: 12021

You are missing some implementations:

1-Create a Searchable Configuration: A searchable configuration defines how the SearchView behaves and is defined in a res/xml/searchable.xml file.

<?xml version="1.0" encoding="utf-8"?>

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

2- In your application's manifest file, declare a <meta-data> element that points to the res/xml/searchable.xml file, so that your application knows where to find it. Declare the element in an <activity> that you want to display the SearchView in:

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

</activity>

3- In the onCreateOptionsMenu() method that you created before, associate the searchable configuration with the SearchView by calling setSearchableInfo(SearchableInfo):

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_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;
}

For further information you can refer to the following links:

Upvotes: 1

Related Questions