Hello all…….

In todays post I will show you how to listen to an EditText input in Android.
Often we need this in our applications.
We implement this with the help of TextWatcher class.

Here is the java code for this.

package com.coderzheaven.test;

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

public class Test extends Activity{
	private EditText ed_txt;

    public void onCreate(Bundle savedInstanceState) {

        ed_txt.addTextChangedListener(new TextWatcher() {
        	public void afterTextChanged(Editable s) {
        		if(ed_txt.getText().toString().trim().length() > 0){
	        		if(Integer.parseInt(ed_txt.getText().toString().trim()) < 20 || Integer.parseInt(ed_txt.getText().toString().trim()) > 120){
						Toast.makeText(getApplicationContext(), "High", Toast.LENGTH_SHORT).show();

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    	    public void onTextChanged(CharSequence s, int start, int before, int count) {

Here I am checking whether the input value in the edittext is between 20 and 120 if yes then change the text color to Green otherwise to Red.

Please leave your valuable comments on this post


One Comment

  1. Jenny February 16, 2012 at 9:58 pm

    Thank u soo much for posting this code…V got d o/p exactly..thank u…


Leave a Reply to Jenny Cancel

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




Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.