Android Simple ListView – Search/Filter

Android RSS : GridView- Downlaod,Parse,Show Headlines With Images and Text
Android RSS : ListView – Downlaod,Parse,Show Headlines With Images and Text
IDG Contributor Network: The top 5 Java 8 features for developers

Android Simple ListView – Search/Filter



Rating : 5/5 (1)




Google search engine is the most visited website in the whole world. This tells us something about us humans and our need to always search and filter through information. The worldwide web is really vast, and we cannot be able to use it conveniently by just remembering website urls or going through portals to find the content we want. Instead we simply search.

Now today we are not developing another search engine. Instead we look at a simple example in android, how to search through a list using searchview. Call it search or filter, but we find the information we want by typing it in the searchview.

SearchView is a widget that was added to android in API 11. It provides s imple edittext where user can enter a query and submit it to the search provider.

For us we simply search through a simple listview using searchview.

 

Common Questions this example explores

  • Android SearchView example.
  • How to search a simple listview in android.
  • Android SearchView arrayadapter example.
  • How to search/filter in android.

Tools Used

This example was written with the following tools:

  • Windows 8.1
  • Eclipse IDE
  • Java Language
  • Bluestacks Emulator

Source Code

Lets have a look at the source code.

MainActivity.javaActivityMain.xmlVideo/Demo

Our MainActivity Class

Purpose :

  1. Reference our ListView and SearchView from xml
  2. Bind our data to ListView using ArrayAdapter.
  3. Handle our SearchView's onQueryTextChangeListener.
  4. Filter or Search dynamically
package com.tutorials.simplelistviewfilter;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
public class MainActivity extends Activity {
  ListView lv;
  SearchView sv;
    String[] teams={"Man Utd","Man City","Chelsea","Arsenal","Liverpool","Totenham"};
  ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView) findViewById(R.id.listView1);
        sv=(SearchView) findViewById(R.id.searchView1);
        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,teams);
        lv.setAdapter(adapter);
        sv.setOnQueryTextListener(new OnQueryTextListener() {
      @Override
      public boolean onQueryTextSubmit(String text) {
        // TODO Auto-generated method stub
        return false;
      }
      @Override
      public boolean onQueryTextChange(String text) {
               adapter.getFilter().filter(text);
        return false;
      }
    });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

 

Our ActivityMain layout

Purpose

  1. Basically display whatever widgets we want to use,in this case ListView and SearchView.
<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=".MainActivity" >
    <SearchView
        android:id="@+id/searchView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:queryHint="Search.."
         >
    </SearchView>
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/searchView1"
        android:layout_marginTop="22dp" >
    </ListView>
</RelativeLayout>

Android ListView Ep.01 - Simple ListView Filter/Search

How To Run

  1. This project was created by Eclipse.
  2. Therefore just copy the code and use in your project in android studio, it's still android anyway and expandableListview is the same regardless of the IDE. So copy paste the code to your android studio project.

More

YouTube

  • Visit our channel for more examples like these.

Facebook

Oclemy,Cheers.

 



Rating :

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    3 × 2 =

    COMMENTS