简体   繁体   中英

Display SearchView in ActionBar Android

I am trying to display a SearchView inside the ActionBar of my activity. I followed android developer guide here here But no SearchView is displayed and I cannot figure out why. Here there is the code of my activity

    public class SearchActivity extends ActionBarActivity {


    private String query;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        FacebookSdk.sdkInitialize(getApplicationContext());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_activity_layout);


        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            this.query = intent.getStringExtra(SearchManager.QUERY);
            Toast.makeText(this,query, Toast.LENGTH_LONG);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);

        return true;
    }

}

here the is the serachable.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" />

here there is the options_menu.xml file

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/search"
        android:title="@string/search_title"
        android:icon="@drawable/ic_action_search"
        android:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView" />
</menu>

and this is the manifest

<activity android:name=".SearchActivity"
              android:label="SearchActivity">

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


    </activity>

the only strange thing is that this line ( android:showAsAction="collapseActionView|ifRoom" ) in the options_menu.xml file is highlighted in red.

Try putting this in your options_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_action_search"
        app:actionViewClass="android.widget.SearchView"
        app:showAsAction="collapseActionView|ifRoom" />
</menu>

And this in your AndroidManifest.xml

<activity android:name=".SearchActivity"
    android:label="SearchActivity">

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

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

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

And this in your SearchActivity.java

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

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);

    return true;
}

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        Log.e("query", intent.getStringExtra(SearchManager.QUERY));
    }
}

For your reference, hope this helps!

menu_main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
    android:title="@string/search_title"
    android:icon="@drawable/ic_search"
    android:showAsAction="collapseActionView|ifRoom"
    android:actionViewClass="android.widget.SearchView" />

\\res\\xml\\searchable.xml:

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

AndroidManifest.xml:

<activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

MainActivity.java:

package com.example.searchview;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, 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;
}

...
}

Try out the following code

menu_search.xml

     <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".SearchActivity">
    <item android:id="@+id/search"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        android:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView" />
</menu>

I think you are making a mistake in the above code. Are you using appcompat library? if no, the above code should work. if yes, try using app:showAsAction instead of android:showAsAction and app:actionViewClass="android.support.v7.widget.SearchView" instead of android:actionViewClass="android.widget.SearchView"

manifest

<activity
        android:name=".SearchActivity"
        android:label="SearchActivity">

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

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

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

    </activity>

SearchActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_search, 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) {
    super.onNewIntent(intent);
    System.out.println("onNewIntent SearchResults");
    handleIntent(intent);
}

private void handleIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        System.out.println("Received query: " + query);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM