Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Search Filter in android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 455
    Comment on it

    In the below example code I have implemented a search functionality to the listview filters item like text or image. Here I have used addTextChangedListener method this will help in search sorting the result like if you type any alphabet in serach box the result starting with taht alphabets will start appreing hence, the search function will filter the listview with a matching string from the user input. In main.xml layout I have added listview,edit text.
    See the below code it will clearly describe you to make Search Filter function

    Step(1)-activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <!-- Editext for Search -->
        <EditText android:id="@+id/inputSearch"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Search products.."
            android:inputType="textVisiblePassword"/>
    
        <!-- List View -->
        <ListView
            android:id="@+id/list_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    Step(2)-Create new list_item.xml layout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
    
        <TextView android:id="@+id/product_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="12dip"
            android:textSize="14dip"
            android:textStyle="bold"/>    
    
    </LinearLayout>
    

    Step(3)public class MainActivity extends Activity {

      private ListView lv;
    
        // Listview Adapter
        ArrayAdapter<String> adapter;
    
        // Search EditText
        EditText inputSearch;
    
    
        // ArrayList for Listview
        ArrayList<HashMap<String, String>> productList;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Listview Data
            String products[] = {"Evon", "Microsoft", "HP", "HCL", "Ericsson",
                                    "Genpack", "Oracle",
                                    "Apple", "Sony", "Wipro", "Dell"};
    
            lv = (ListView) findViewById(R.id.list_view);
            inputSearch = (EditText) findViewById(R.id.inputSearch);
    
            // Adding items to listview
            adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
            lv.setAdapter(adapter);
             //Search Filter
    
            inputSearch.addTextChangedListener(new TextWatcher() {
    
                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                    MainActivity.this.adapter.getFilter().filter(cs);   
                }
    
                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                        int arg3) {
    
                }
    
                @Override
                public void afterTextChanged(Editable arg0) {
    
                }
            });
        }    
    }
    

    Step(4) Add property in your AndroidManifest.xml-

    [android:windowSoftInputMode="stateHidden"]

          <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.tiwari.rajshekhar.filter"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="15" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="Filter"
                android:windowSoftInputMode="stateHidden">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: