Android Material SearchView and History

Android RSS : ListView – Downlaod,Parse,Show Headlines With Images and Text
Android RSS : GridView- Downlaod,Parse,Show Headlines With Images and Text
Android RSS : ListView Multiple Fields – Download,Parse,Show Headlines

Android Material SearchView and History







  • Here the purpose is to programmatically open search,close and show search suggestions while searching.
  • The search occurs in a nice fragment.
  • We use a Material SearchView library.

Please download the above source code for full reference.

First we add the Material SearchView library.

apply plugin: 'com.android.application'
android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"
    defaultConfig {
        applicationId "com.tutorials.hp.materialsearchgridview"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:design:24.1.1'
    compile 'br.com.mauker.materialsearchview:materialsearchview:1.1.0'
}

 

Second we create a package called br.com.mauker as below and the following class :

 

package br.com.mauker;
/**
 * Created by Oclemy on 9/5/2016 for ProgrammingWizards Channel and http://www.camposha.com.
 */
public class MsvAuthority {
    public static final String CONTENT_AUTHORITY = "br.com.mauker.materialsearchview.searchhistorydatabase";
}

 

Here's our MainActivity class :

 

 

package com.tutorials.hp.materialsearchgridview;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import br.com.mauker.materialsearchview.MaterialSearchView;
public class MainActivity extends AppCompatActivity {
   MaterialSearchView msv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        msv= (MaterialSearchView) findViewById(R.id.msv);
        msv.addSuggestions(new String[]{"Casini","Andromeda","Europa","Voyager","Spitzer","Hubble"});
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                msv.openSearch();
            }
        });
    }
    @Override
    public void onBackPressed() {
       if(msv.isOpen())
       {
           msv.closeSearch();
       }else
       {
           super.onBackPressed();
       }
    }
}

 

Then in our layout we add Material searchView as below :

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.tutorials.hp.materialsearchgridview.MainActivity"
    tools:showIn="@layout/activity_main">
    <br.com.mauker.materialsearchview.MaterialSearchView
        android:id="@+id/msv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

 

 

For more details and demo,check the video tutorial below :

Android Search/Filter Ep.01 : Material SearchView - Open and Show Search Suggestions

 



    Leave a Reply

    Your email address will not be published. Required fields are marked *

    fourteen − 9 =

    COMMENTS