null
null

Reputation: 11

Android SearchView widget does not work

I have a problem getting the searchview in the actionbar working...

The issue is, that my app shows the searchview but when I attempt to search nothing happens.

Here is the SearchActivity class:

public class MainActivity extends AppCompatActivity
    implements SearchListFragment.OnCardSelectedListener
{
    private static final String TAG = "MainActivity";
    private static final String idLanguage = "1";

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

        // Check that the activity is using the layout version with
        // the fragment_container FrameLayout
        if (findViewById(R.id.body_fragment) != null)
        {
            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null)
            {
                return;
            }
        }
    }

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

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

        searchView.setIconifiedByDefault(false);            

        return super.onCreateOptionsMenu(menu);
    }
}

Here is my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="eu.prindl.magiccardmarket"
      android:versionCode="1"
      android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="20" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

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

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

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

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <activity
            android:name=".SearchResultsActivity"
            android:launchMode="singleTop"
            android:parentActivityName=".MainActivity" >

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

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

        </activity>

    </application>

</manifest>

And finally here is my SearchActivity class:

package eu.mcm.prindl.magiccardmarket;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;

/**
 * Created by konstantin on 8/5/15.
 */

public class SearchResultsActivity extends Activity
{
    private static final String TAG = "SearchResultsActivity";

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_result_layout);

        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent (Intent intent)
    {
        //super.onNewIntent(intent);
        setIntent(intent);
        handleIntent(intent);
    }

    /**
     * Handling intent data
     */
    private void handleIntent (Intent intent)
    {
        if (Intent.ACTION_SEARCH.equals(intent.getAction()))
        {
            String query = intent.getStringExtra(SearchManager.QUERY);
            Debug.write(TAG,"Search Query: " + query);

        }

    }
}

Please help. I have looked for various solutions on Stackoverflow but none seemed to work (but maybe I just missed some things).

UPDATE: Here is my searchable.xml:

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

Upvotes: 1

Views: 1372

Answers (1)

Nick Cardoso
Nick Cardoso

Reputation: 21773

Your manifest should look more like the following with what it contains:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="...">
<application android:name="..." android:icon="@drawable/logo" android:label="@string/app_name" android:theme="@style/...">
    <activity android:name=".MainActivity" android:label="@string/app_name"
        android:launchMode="singleTop" android:theme="@style/AppTheme">
        <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
        <intent-filter>
             <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        ...
    </activity>
    ...
</application>

With the matching XML file containing something like

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

Also make sure your Activity actually sets up the Search View:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    if (searchView != null) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                //TODO: Reset your views
                return false;
            }
        });
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String s) {
                    return false; //do the default
                }

                @Override
                public boolean onQueryTextChange(String s) {
                    //NOTE: doing anything here is optional, onNewIntent is the important bit
                    if (s.length() > 1) { //2 chars or more
                        //TODO: filter/return results
                    } else if (s.length() == 0) {
                        //TODO: reset the displayed data
                    }
                    return false;
                }

            });
        }
    }
    return true;
}

Upvotes: 2

Related Questions