What is searchview

Android SearchView is a widget that we can use for search the query.if it will find the match from the provider then it will display the match other wise it will display no match.

In my program i going to use the SearchView to search the name of person from the listview that is the provider.


SearchView:.

In this section i am going to show the SearchView with practical example.

First open the android studio and create a project and give the name of your project like SearchViewDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop the one ListView and one SearchView from pallet. Open the activity_main.xml file and code look like this


<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"
    tools:context="com.example.niitpc.myapplication.MainActivity">


    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="30dp" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/searchView"
        android:layout_marginTop="38dp" />
</RelativeLayout>

     

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.myapplication;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
ListView listView;
SearchView searchView;
ArrayList arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listView);
searchView=(SearchView)findViewById(R.id.searchView);
arrayList = new ArrayList<>();
arrayList.add("Ram");
arrayList.add("Shyam");
arrayList.add("Amit");
arrayList.add("Amita");
arrayList.add("Shashank");
arrayList.add("Nitin");
arrayList.add("Chanchal");

final ArrayAdapter adapter=new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,arrayList);
listView.setAdapter(adapter);
searchView.setOnQueryTextListener(

new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if(arrayList.contains(query))
{
  adapter.getFilter().filter(query);

}
else{

Toast.makeText(MainActivity.this,"No match", Toast.LENGTH_LONG).show();
}
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
);

}


}


       

Explanation:

ListView listView:This statement create the object listview of ListView.

String[] Names=new String[]{"Ram","Shyam","Amit","Amita"};:This statement create the String array name is Names store the name of person.

listView=(ListView)findViewById(R.id.listView)This statement initialized the listview object.

ArrayAdapter adapter=new ArrayAdapter(this,R.layout.name_list,Names):This statement create the object adapter of ArrayAdapter.It will take three argument first as this second as layout that you create like name_list and third is array Names that store the names of persion.

listView.setAdapter(adapter):The setAdapter() set the adpter object into listview.

listView.setOnItemClickListener:This statement create the listener of listview.

String value=(String) listView.getItemAtPosition(position):The getItemAtPosition() method take a position as argument and return the click item from the listview and after converting into String we store into variable value.

Toast.makeText(MainActivity.this,"Position"+position+"Value"+value,Toast.LENGTH_LONG).show():The Toast display the position and value onto the screen with touch the name in listview.

String url=editText.getText().toString():The getText() return the enter url after converting it into String value assign into the url variable.


SearchView

What is searchview for video Click here

Smiley face                     Smiley face