Skip to content Skip to sidebar Skip to footer

How To Add An Android Searchbar1

I was wondering if anyone could break down for me step by step how to add a searchbar in an activity . I have tried to look it up online but I couldn't really understand how it's d

Solution 1:

Add this dependency in your gradle :

dependencies {
    compile'com.miguelcatalan:materialsearchview:1.0.0'
}

Add this to your Mani Activity:

<RelativeLayoutandroid:id="@+id/toolbar_container"android:layout_width="match_parent"android:layout_height="wrap_content"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="@color/theme_primary" /><com.miguelcatalan.materialsearchview.MaterialSearchViewandroid:id="@+id/search_view"android:layout_width="match_parent"android:layout_height="wrap_content" /></RelativeLayout>

Create a menu file:

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_action_search"
    android:orderInCategory="100"
    android:title="@string/abc_search_hint"
    app:showAsAction="always" />

Add this to your MainActivity:

MaterialSearchView searchView = (MaterialSearchView) findViewById(R.id.search_view);
searchView.setOnQueryTextListener(newMaterialSearchView.OnQueryTextListener() {
        @OverridepublicbooleanonQueryTextSubmit(String query) {
            //Do some magicreturnfalse;
        }

        @OverridepublicbooleanonQueryTextChange(String newText) {
            //Do some magicreturnfalse;
        }
    });

    searchView.setOnSearchViewListener(newMaterialSearchView.SearchViewListener() {
        @OverridepublicvoidonSearchViewShown() {
            //Do some magic
        }

        @OverridepublicvoidonSearchViewClosed() {
            //Do some magic
        }
    });

Here is OnActivityResult:

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MaterialSearchView.REQUEST_VOICE && resultCode == RESULT_OK) {
        ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        if (matches != null && matches.size() > 0) {
            StringsearchWrd= matches.get(0);
            if (!TextUtils.isEmpty(searchWrd)) {
                searchView.setQuery(searchWrd, false);
            }
        }

        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

Create some array list in string res:

<string-arrayname="query_suggestions"><item>Android</item><item>iOS</item><item>SCALA</item><item>Ruby</item><item>JavaScript</item></string-array>

Add the string res to search textbox:

searchView.setSuggestions(getResources().getStringArray(R.array.query_suggestions));

Solution 2:

add a new menu file and name it as res/menu/menu_search.xml:

<menuxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><itemandroid:id="@+id/search"android:title="@string/hint_search"android:icon="@android:drawable/ic_menu_search"app:showAsAction="collapseActionView|ifRoom"app:actionViewClass="android.support.v7.widget.SearchView" /></menu>

Once we create a menu file, we're going to load it in the MainActivity like described below:

publicclassMainActivityextendsAppCompatActivityimplementsSearchView.OnQueryTextListener {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);

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

        returntrue;
    }

Post a Comment for "How To Add An Android Searchbar1"