Wednesday, August 19, 2015

How to use EditText TextChangedListener in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#eae8e3"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-condensed"
        android:padding="15dp"
        android:layout_margin="15dp"
        android:textSize="20dp"
        />
    <EditText
        android:id="@+id/et"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:hint="This is initial text"
        android:layout_below="@id/tv"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

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


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        final EditText et = (EditText) findViewById(R.id.et);
        final TextView tv = (TextView) findViewById(R.id.tv);

        /*
            addTextChangedListener (TextWatcher watcher)
                Adds a TextWatcher to the list of those whose methods
                are called whenever this TextView's text changes.

                In 1.0, the afterTextChanged(Editable) method was erroneously
                not called after setText(char[], int, int) calls. Now, doing
                setText(char[], int, int) if there are any text changed
                listeners forces the buffer type to Editable if it
                would not otherwise be and does call this method.
         */

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

                afterTextChanged(Editable s)
                    This method is called to notify you that,
                    somewhere within s, the text has been changed.

                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.

                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.
         */

        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // Do some thing now
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // Change the TextView background color
                tv.setBackgroundColor(Color.YELLOW);

                // Get the EditText text and display it on TextView
                tv.setText(et.getText());
            }

            @Override
            public void afterTextChanged(Editable s) {
                // Do something at this time
            }
        });
    }
}



More android examples