android-text-watcher

Android TextWatcher Example

Android TextWatcher Example

TextWatcher in android is used to get instant insert into EditText. TextWatcher has three method those need to override in our application.

1.  afterTextChanged (Editable s)

2. beforeTextChanged (CharSequence s, int start, int count, int after)

3.  onTextChanged (CharSequence s, int start, int before, int count)

afterTextChanged :- This method return value when user complete his typing to the edittext.

beforeTextChanged:-  This method return value when user about to make change value to the edittext.

onTextChanged:- This method return instance result when user type to the edittext.

 

 

SourceCode:-


TextWatcherActivity.java


 

package com.androidfromhome.textwatcher;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class TextWatcherActivity extends Activity {

    private EditText et_write;
    private TextView tv_display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_watcher);
        
        et_write=(EditText)findViewById(R.id.et_write);
        tv_display=(TextView)findViewById(R.id.tv_display);
        
        et_write.addTextChangedListener(textWatcher);
        
    }
    TextWatcher textWatcher = new TextWatcher(){
        
       
    
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onTextChanged(CharSequence s, int a, int b, int c) {
             // TODO Auto-generated method stub           
            tv_display.setText(s);    
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            
        }
        
    
    };
    
}

 


 

activity_text_watcher.xml


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidfromhome.textwatcher.TextWatcherActivity" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="8dp" >

        <TextView
            android:id="@+id/tv_display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:textSize="16dp" />

        <EditText
            android:id="@+id/et_write"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="#dfdfdf"
            android:ems="10"
            android:padding="10dp" />

    </LinearLayout>

</LinearLayout>

 


 

 

 

Related Post:-

Android Autostart App

Font Style For Android

Sqlite database example in android

ListView With section header in android example

Leave a Reply

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

Get Widget