android searchview
ReportQuestion
2 years ago 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.
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
- public boolean onQueryTextSubmit(String query): It searches the query on the submission of content over SearchView editor. It is case dependent.
- public boolean onQueryTextChange(String newText): It searches the query at the time of text change over SearchView editor.
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 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"
- tools:context="com.example.test.searchview.MainActivity">
- <ListView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/lv1"
- android:divider="#ad5"
- android:dividerHeight="2dp"
- android:layout_below="@+id/searchView"/>
- <SearchView
- android:id="@+id/searchView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:queryHint="Search Here"
- android:iconifiedByDefault="false"
- android:layout_alignParentTop="true"
- />
- </RelativeLayout>
MainActivity.java
- package com.example.test.searchview;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.ArrayAdapter;
- import android.widget.Filter;
- import android.widget.ListView;
- import android.widget.SearchView;
- import android.widget.Toast;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- SearchView searchView;
- ListView listView;
- ArrayList<String> list;
- ArrayAdapter<String > adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- searchView = (SearchView) findViewById(R.id.searchView);
- listView = (ListView) findViewById(R.id.lv1);
- list = new ArrayList<>();
- list.add("Apple");
- list.add("Banana");
- list.add("Pineapple");
- list.add("Orange");
- list.add("Lychee");
- list.add("Gavava");
- list.add("Peech");
- list.add("Melon");
- list.add("Watermelon");
- list.add("Papaya");
- adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
- listView.setAdapter(adapter);
- searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
- @Override
- public boolean onQueryTextSubmit(String query) {
- if(list.contains(query)){
- adapter.getFilter().filter(query);
- }else{
- Toast.makeText(MainActivity.this, "No Match found",Toast.LENGTH_LONG).show();
- }
- return false;
- }
- @Override
- public boolean onQueryTextChange(String newText) {
- // adapter.getFilter().filter(newText);
- return false;
- }
- });
- }
- }
Thread Reply