Home Ask Login Register

Developers Planet

Your answer is one click away!

Ji Yicheng February 2016

Add the Search View to the App Bar in Android

I am trying to add the search view to the app bar in Android. Followed by this tutorial:http://developer.android.com/training/search/setup.html#add-sv

but when i run the program, it is only a icon could be clicked, the search view did not appear, but the tutorial says it should be appear. Can anyone help me?

menu_main.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/action_search"
    android:title="@string/abc_search_hint"
    android:icon="@drawable/ic_search_white_48dp"
    app:showAsAction="ifRoom|collapseActionView"
    android:actionViewClass="android.widget.SearchView" />
    </menu>



    MainActivity

   public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_search) {
        return true;
    }
    return true;
}

here is the screenshot enter image description here

Answers


IgorB February 2016

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"
            android:icon="@drawable/ic_search_white"
            android:textColor="@color/white"
            app:showAsAction="ifRoom|collapseActionView"
            app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

Activity

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_menu, menu);
        MenuItem searchItem = menu.findItem(R.id.search);
        final SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) MenuItemCompat.getActionView(searchItem);

        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
                if (mSearchTerm == null && newFilter == null) {
                    return true;
                }
                if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                    return true;
                }
                mSearchTerm = newFilter;
                mSearchQueryChanged = true;
                searchText(newText); //handle this
                return true;
            }
        });

        MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCom 

Post Status

Asked in February 2016
Viewed 1,405 times
Voted 4
Answered 1 times

Search




Leave an answer


Quote of the day: live life