Tuesday, March 16, 2021

Android EditText With TextWatcher

 

Android EditText with TextWatcher (Searching data from ListView)

Android EditText is a subclass of TextView. EditText is used for entering and modifying text. While using EditText width, we must specify its input type in inputType property of EditText which configures the keyboard according to input.

EditText uses TextWatcher interface to watch change made over EditText. For doing this, EditText calls theaddTextChangedListener() method.

Methods of TextWatcher

  1. beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3): It is executed before making any change over EditText.
  2. onTextChanged(CharSequence cs, int arg1, int arg2, int arg3): It is executed while making any change over EditText.
  3. afterTextChanged(Editable arg0): It is executed after change made over EditText.

Example of EditText with TextWatcher()

In this example, we will implement EditText with TextWatcher to search data from ListView.

activity_main.xml

Create an activity_main.xml file in layout folder containing EditText and ListView.

File: activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="com.example.test.searchfromlistview.MainActivity">  
  11.               
  12.     <EditText  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/editText"  
  16.         android:inputType="text"  
  17.         android:layout_alignParentTop="true"  
  18.         android:layout_alignParentLeft="true"  
  19.         android:layout_alignParentStart="true"  
  20.         android:layout_alignParentRight="true"  
  21.         android:layout_alignParentEnd="true" />  
  22.   
  23.     <ListView  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:id="@+id/listView"  
  27.         android:layout_below="@+id/editText"  
  28.         android:layout_alignParentLeft="true"  
  29.         android:layout_alignParentStart="true" />  
  30. </RelativeLayout>  

Create another file list_item.xml in layout folder which contains data of ListView.

list_item.xm

File: list_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent">  
  7.   
  8. <TextView android:id="@+id/product_name"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content"  
  11.     android:padding="10dip"  
  12.     android:textSize="16dip"  
  13.     android:textStyle="bold"/>  
  14. </LinearLayout>  

Activity class

Activity class

  1. package com.example.test.searchfromlistview;  
  2.   
  3. import android.os.Bundle;  
  4. import android.text.Editable;  
  5. import android.text.TextWatcher;  
  6. import android.widget.ArrayAdapter;  
  7. import android.widget.EditText;  
  8. import android.widget.ListView;  
  9. import android.support.v7.app.AppCompatActivity;  
  10. import android.widget.Toast;  
  11.   
  12. public class MainActivity extends AppCompatActivity {  
  13.   
  14.     private ListView lv;  
  15.     private EditText editText;  
  16.     private ArrayAdapter<String> adapter;  
  17.   
  18.     private String products[] = {"Apple""Banana","Pinapple""Orange""Papaya""Melon",  
  19.             "Grapes""Water Melon","Lychee""Guava""Mango""Kivi"};  
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.   
  25.         lv = (ListView) findViewById(R.id.listView);  
  26.         editText = (EditText) findViewById(R.id.editText);  
  27.         adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);  
  28.         lv.setAdapter(adapter);  
  29.   
  30.         editText.addTextChangedListener(new TextWatcher() {  
  31.   
  32.             @Override  
  33.             public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {  
  34.                 adapter.getFilter().filter(cs);  
  35.             }  
  36.   
  37.             @Override  
  38.             public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {  
  39.                 Toast.makeText(getApplicationContext(),"before text change",Toast.LENGTH_LONG).show();  
  40.             }  
  41.   
  42.             @Override  
  43.             public void afterTextChanged(Editable arg0) {  
  44.                 Toast.makeText(getApplicationContext(),"after text change",Toast.LENGTH_LONG).show();  
  45.             }  
  46.         });  
  47.     }  
  48. }  

Output

android edittext 1 android edittext 2 

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile