SearchView Example

search icon is used for search all the contents in this page. Finally we implements SearchView.OnQueryTextListener to filter the list according to search query.


This xml shows listview with searchview option.




This activity adds the code to initiate SearchView and ListView. We also implements SearchView.OnQueryTextListener to filter lists according to search query.

package com.example.codebridgeexample;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.SearchView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {

    ListView listView;
    ListViewAdapter listViewAdapter;
    SearchView searchView;
    String[] countryList;
    ArrayList arraylist = new ArrayList();

    public void onCreate(Bundle savedInstanceState) {

        countryNameList = new String[]{"India", "Australia", "Japan",
                "China", "Hongkong", "Russia", "Europe", "France",

        listView = (ListView) findViewById(;

        for (int i = 0; i < countryNameList.length; i++) {
            CountryNames countryNames = new CountryNames(countryNameList[i]);
            arraylist.add(countryNames );

        listViewAdapter= new ListViewAdapter(this, arraylist);

        editsearch = (SearchView) findViewById(;

    public boolean onQueryTextSubmit(String query) {

        return false;

    public boolean onQueryTextChange(String newText) {
        String text = newText;
        return false;

we extends baseadapter  in ListViewAdapter class and then set the data in the ListView by using Modal class.

package com.example.codebrdigeexample;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class ListViewAdapter extends BaseAdapter {

    Context mContext;
    LayoutInflater layoutInflater;
    private List countryNamesList = null;
    private ArrayList arraylist;

    public ListViewAdapter(Context context, List countryNamesList) {
        mContext = context;
        this.countryNamesList= countryNamesList;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList();

    public class ViewHolder {
        TextView name;

    public int getCount() {
        return countryNamesList.size();

    public CountryNames getItem(int position) {
        return countryNamesList.get(position);

    public long getItemId(int position) {
        return position;

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.listView_item, null);
   = (TextView) view.findViewById(;
        } else {
            holder = (ViewHolder) view.getTag();
        return view;

    // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        if (charText.length() == 0) {
        } else {
            for (CountryNames cn : arraylist) {
                if (cn.getAnimalName().toLowerCase(Locale.getDefault()).contains(charText)) {



This xml shows code for each row displayed based on listview adapter




We have a constructor for setting the country name and a function to get the country name.

package com.example.codebrdigeexample;

public class CountryNames {
private String countryName;

public CountryNames(String countryName) {
this.countryName = countryName;

public String getCountryName() {
return this.countryName;