Listview with Searchview in Android Example |Android App Development With AndroidCource

Wednesday 4 July 2018

Listview with Searchview in Android Example

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

Search view Methods


onQueryTextSubmit(String query)

In this method use to search query submit and then display searchable item.you can enter searchview data and click enter to display searchable item.

onQueryTextChange(String newText)

In this method use to search query at the time of text change over SearchView editor.

MainActivity.java

<?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"
    tools:context=".MainActivity">

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:queryHint="Search Version"
        android:iconifiedByDefault="false"
        android:layout_alignParentTop="true"
        />

    <ListView
        android:id="@+id/list_view"
        android:layout_below="@id/searchView"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</RelativeLayout>

MainActivity.java

package com.example.harsh.listviewradiobutton;

import android.support.v4.widget.SwipeRefreshLayout;
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.CompoundButton;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    ArrayList<String> list;
    ArrayAdapter adapter;
    String[] version = {"Aestro","Blender","CupCake","Donut","Eclair","Froyo","GingerBread","HoneyComb","IceCream Sandwich",
    "Jelly Bean","Kitkat","Lolipop","Marshmallow","Nought","Oreo"};
    SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.list_view);
        searchView = findViewById(R.id.searchView);

        list = new ArrayList<>();

        for (int i = 0;i<version.length;i++){

            list.add(version[i]);

        }

        adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Toast.makeText(MainActivity.this, "Selected -> " + version[i], Toast.LENGTH_SHORT).show();
            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {

                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                if(list.contains(s)){
                    adapter.getFilter().filter(s);
                }
                return true;
            }
        });
    }

}

Searchview in android example.,searchview with listview in androidSearchview in android example.,searchview with listview in android

0 comments:

Post a Comment