Saturday 3 August 2013

Search Bar in ListView Android

1 comment
We have made list many a time’s in android, in this tutorial I will be adding search bar functionality to the ListView. In this project I have added a search bar above the list. ListView will be filtered on the basis of text added on the search bar. It is useful where you have a  long list.


Step 1:- create activity_main.xml
<LinearLayout 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:orientation="vertical">
   <EditText
       android:id="@+id/search_editText"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="Search"/>
  
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       >
    </ListView>
</LinearLayout>

Step 2:- create list_row.xml for particular row
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold" >
</TextView>

Step 3:- MainActivity.java
package com.arpit.searchbarandroidlistview;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
    ListView lv;
    EditText etsearch;
    ArrayAdapter<String> adapter;
    List<String> arr;
    String[] language = { "C", "Java", "C++", "C-sharp", ".Net", "SQL",
            "Android", "PhoneGap", "Ios", "windows", "PHP", "Phyton", "Perl" };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.listView1);
        etsearch = (EditText) findViewById(R.id.search_editText);
        arr = new ArrayList<String>(Arrays.asList(language));
        adapter = new ArrayAdapter<String>(this, R.layout.list_row, arr);
        lv.setAdapter(adapter);
       
      // code for searching from list
 
etsearch.addTextChangedListener(new TextWatcher() {
           
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                 MainActivity.this.adapter.getFilter().filter(s);
               
            }
           
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
               
            }
           
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
               
            }
        });       
           }
}



Catch Me On: CreateAppFaster.com

1 comment:

  1. Ok...

    What I'm going to tell you may sound a little creepy, maybe even a little "supernatural"

    BUT what if you could just hit "PLAY" and LISTEN to a short, "magical tone"...

    And suddenly attract MORE MONEY to your life??

    And I'm talking about thousands... even MILLIONS of DOLLARS!!

    Sounds way too EASY?? Think something like this is not for real??

    Well, I've got news for you..

    Many times the most magical miracles life has to offer are the easiest to GET!!

    Honestly, I will PROVE it to you by letting you PLAY a REAL "miracle money-magnet tone" I've synthesized...

    You just push "PLAY" and watch how money starts piling up around you... it starts right away...

    TAP here NOW to PLAY this mysterious "Miracle Abundance Tone" - as my gift to you!!

    ReplyDelete