Android editText TextWatcher code latest example.

Android editText TextWatcher example source code beginner

TextWatcher in EditText required when you want to alert app user what to write in this particular editText field. Suppose editText is used as password field and application alerting password length need number symbol must etc. Below is source code of Android Edittext TextWatcher for MainActivity.java copy this code paste it in your project.

 

package write your package name here;

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

public class MainActivity extends Activity {

private EditText editTextWatcher;
private TextView textView;

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

editTextWatcher = (EditText) findViewById(R.id.passwordSize);
textView = (TextView) findViewById(R.id.strength);
textView.setVisibility(View.GONE);

//Listener for textWatcher
editTextWatcher.addTextChangedListener(textActivityWatcher);
}

private final TextWatcher textActivityWatcher = new TextWatcher() {

public void beforeTextChanged(CharSequence s, int begin, int count, int next) {
//add some toast or text showed in textView
}

public void onTextChanged(CharSequence s, int begin, int last, int count) {
textView.setVisibility(View.VISIBLE);

textView.setText(s);
if(begin == 3){
Toast.makeText(getApplicationContext(), “Add more text required”, Toast.LENGTH_SHORT).show();
}
else if (begin == 5){
Toast.makeText(getApplicationContext(), “Add few more text required”, Toast.LENGTH_SHORT).show();
}
else if(begin == 7) {
Toast.makeText(getApplicationContext(), “Max limit is 9 add few more”, Toast.LENGTH_SHORT).show();
}
}

public void afterTextChanged(Editable ed) {
if (ed.length() == 0) {
textView.setVisibility(View.INVISIBLE);
}
else{
textView.setText(“You write this : ” + editTextWatcher.getText());
}
}
};
}

 

Android editText TextWatcher activity_main.xml code. Visit here to get more android tutorial latest code.

 

activity_main.xml code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:text="Enter your password and see the magic!!"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="123dp" />

    <EditText
        android:id="@+id/passwordSize"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:inputType="textPassword"
        android:padding="12dp"
        android:layout_above="@+id/strength"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="75dp">

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/strength"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffd33a26"
        android:padding="5dp"
        android:text="* Not Entered"
        android:textColor="#fff"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="70dp" />
</RelativeLayout>

Add a Comment

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