android searchview

Report
Question
686 views

Android SearchView provides user interface to search query submitted over search provider. SearchView widget can be implemented over ToolBar/ActionBar or inside a layout.

Please explain why do you think this question should be reported?

Report Cancel

SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class. For making search field visible, SearchView uses setIconifiedByDefault(false) method.

Methods of SearchView

  1. public boolean onQueryTextSubmit(String query): It searches the query on the submission of content over SearchView editor. It is case dependent.
  2. public boolean onQueryTextChange(String newText): It searches the query at the time of text change over SearchView editor.

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="com.example.test.searchview.MainActivity">  
  11.   
  12.     <ListView  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="match_parent"  
  15.         android:id="@+id/lv1"  
  16.         android:divider="#ad5"  
  17.         android:dividerHeight="2dp"  
  18.         android:layout_below="@+id/searchView"/>  
  19.   
  20.     <SearchView  
  21.         android:id="@+id/searchView"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:queryHint="Search Here"  
  25.         android:iconifiedByDefault="false"  
  26.         android:layout_alignParentTop="true"  
  27.    />  
  28.   
  29. </RelativeLayout>  
     

MainActivity.java

  1. package com.example.test.searchview;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.widget.ArrayAdapter;  
  6. import android.widget.Filter;  
  7. import android.widget.ListView;  
  8. import android.widget.SearchView;  
  9. import android.widget.Toast;  
  10.   
  11. import java.util.ArrayList;  
  12.   
  13. public class MainActivity extends AppCompatActivity {  
  14.     SearchView searchView;  
  15.     ListView listView;  
  16.     ArrayList<String> list;  
  17.     ArrayAdapter<String > adapter;  
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.   
  23.         searchView = (SearchView) findViewById(R.id.searchView);  
  24.         listView = (ListView) findViewById(R.id.lv1);  
  25.   
  26.         list = new ArrayList<>();  
  27.         list.add("Apple");  
  28.         list.add("Banana");  
  29.         list.add("Pineapple");  
  30.         list.add("Orange");  
  31.         list.add("Lychee");  
  32.         list.add("Gavava");  
  33.         list.add("Peech");  
  34.         list.add("Melon");  
  35.         list.add("Watermelon");  
  36.         list.add("Papaya");  
  37.   
  38.         adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);  
  39.         listView.setAdapter(adapter);  
  40.   
  41.         searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {  
  42.             @Override  
  43.             public boolean onQueryTextSubmit(String query) {  
  44.   
  45.                 if(list.contains(query)){  
  46.                     adapter.getFilter().filter(query);  
  47.                 }else{  
  48.                     Toast.makeText(MainActivity.this, "No Match found",Toast.LENGTH_LONG).show();  
  49.                 }  
  50.                 return false;  
  51.             }  
  52.   
  53.             @Override  
  54.             public boolean onQueryTextChange(String newText) {  
  55.             //    adapter.getFilter().filter(newText);  
  56.                 return false;  
  57.             }  
  58.         });  
  59.     }  
  60. }  

Thread Reply

Leave an comment

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>