Android Material ToolBar SearchBar/SearchView Example – How to filter search a simple GridView.

It’s true we have looked at several searchview examples previously. However, we have been using the ordinary searchview that we normally display in our content section.

A more popular approach is using a searchview/searchbar placed in the toolbar or appbar of our activity. It’s what we look at in this example. We use a toolbar searchbar to search/filter our simple GridView.


  • Here’s the screenshot of the project.

Android ToolBar SearchBar Android ToolBar SearchBar

Android ToolBar Material SearchBar Project Structure Android ToolBar Material SearchBar

Common Questions this example explores

  • Android Material ToolBar SearchBar example.
  • Android Filter Search a GridView.

Tools Used

This example was written with the following tools:

  • Windows 8
  • AndroidStudio IDE
  • Genymotion Emulator
  • Language : Java
  • Topic : Material SearchBar, ToolBar SearchBar, GridView Search Filter

Libaries Used

Source Code

Lets jump directly to the source code.

Build.Gradle Project Level

  • Our project level build.gradle.
  • We add repositories for fetching our dependencies here.
  • The dafult,jccenter() is already specified.
  • However, we add the maven and specify the url we’ll use to fetch our Material SearchBar here.
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
    dependencies {
        classpath ''

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files

allprojects {
    repositories {
        maven { url "" }


task clean(type: Delete) {
    delete rootProject.buildDir

Build.Gradle App Level

  • Our app level build.gradle file.
  • We specify compilesdk,minimum sdk,target sdk and dependencies.
  • Note that the minimum sdk must be API level 16 Jelly bean for us to use the Material Searchbar library that we use here.
  • We also add dependencies using ‘compile’ statement.
  • Our activity shall derive from the appCompatActivity to make it target earlier android versions.
  • We also specify design support library.
  • Finally we add the com.github.mancj:MaterialSearchBar which is a third party library we’ll use to display Material Searchbar.
  • You then sync the project.
apply plugin: ''

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.tutorials.hp.searchbargridview"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner ""
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile ''
    compile ''
    compile ''
    compile 'com.github.mancj:MaterialSearchBar:0.7.1'

  • Our MainActivity class.
  • Derives from AppCompatActivity which is a Base class for activities that use the support library action bar features.
  • Methods: onCreate().
  • Inflated From activity_main.xml using the setContentView() method.
  • This method is responsible for filtering/searching a gridview via a material serahcbar displayed inside a toolbar.
  • The views we use are Material Searchbar for searching and GridView to be searched.
  • Reference MaterialSearchBar and GridView from our layout specification using findViewById().
  • Instantiate adapter and set it to GridView.
  • Add TextChangeListener to MaterialSearchBar and use arrayadapter instance to filter data.


  • ActivityMain.xml.
  • This is a template layout for our MainActivity.
  • Root layout tag is CoordinatorLayout from design support library.
  • CordinatorLayout is viewgroup that is a superpowered on framelayout.
  • CoordinatorLayout is intended for two primary use cases: As a top-level application decor or chrome layout As a container for a specific interaction with one or more child views
  • Inside our CordinatorLayout we add : AppBarLayout,FloatingActionButton and include content_main.xml.
  • AppBarLayout is vertical LinearLayout that implements scrolling features of material design concept.
  • It should be a direct child of CordinatorLayout, otherwise alot of features won’t work.
  • Inside the AppBarLayout we add our toolbar,which we give a blue color.
  • Next we add our Material SearchBar which will give us the searchbar.
  • We can specify attributes like style and hint.
  • We will add our widgets in our content_main.xml, not here as this is a template layout.
  • Finally we have a FloatingActionButton, a class that derives from Its the round button you see in our user interface.


  • Our ContentMain.xml file.
  • Shall get inflated to MainActivity.
  • Root tag is relativeLayout.
  • Contains GridView with two columns.
  • It is this gridview that we shall be searching.


  • We have a YouTube channel with almost a thousand tutorials, this one below being one of them.


  • You can Download the full Project below. Source code is well commented.


How To Run

  1. Download the project above.
  2. You’ll get a zipped file,extract it.
  3. Open the Android Studio.
  4. Now close, already open project.
  5. From the Menu bar click on File >New> Import Project.
  6. Now Choose a Destination Folder, from where you want to import project.
  7. Choose an Android Project.
  8. Now Click on “OK“.
  9. Done, your done importing the project,now edit it.



  • Visit our channel for more examples like these.