Friday, 22 May 2015

Android Textwatcher

TextWatcher

When an object of a type is attached to an Editable, its methods will be called when the text is changed.

public abstract void afterTextChanged (Editable s)

   This method is called to notify you that, somewhere within s, the text has been changed. It is legitimate to make further changes to s from this callback, but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively. (You are not told where the change took place because other afterTextChanged() methods may already have made other changes and invalidated the offsets. But if you need to know here, you can use setSpan(Object, int, int, int) in onTextChanged(CharSequence, int, int, int) to mark your place and then look up from here where the span ended up.


public abstract void beforeTextChanged (CharSequence s, int start, int count, int after)
  
   This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with length after. It is an error to attempt to make changes to s from this callback.


public abstract void onTextChanged (CharSequence s, int start, int before, int count)

  This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length before. It is an error to attempt to make changes to s from this callback.


Example:

Step 1)Open res->layout->activity_main.xml;

    <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="com.example.android_textwatcher.MainActivity" >

    <TextView
        android:id="@+id/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Dhina Textwatcher Example" />

    <EditText
        android:id="@+id/editext1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/android"
        android:layout_marginTop="50dp"
        android:textColor="#000000" />

        android:id="@+id/editext2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editext1"
        android:layout_marginTop="50dp"
        android:textColor="#000000" />

</RelativeLayout >

Step 2)Activity Class
  
package com.example.android_textwatcher;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

public class MainActivity extends Activity 
{

EditText editext1,editext2;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editext1=(EditText)findViewById(R.id.editext1);
editext2=(EditText)findViewById(R.id.editext2);
editext1.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
editext2.setText(new String(s.toString()));

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

editext2.setText(new String(s.toString()));
}

@Override
public void afterTextChanged(Editable s) {

editext2.setText(new String(s.toString()));
}

});
}

}

Happy Coding :)