Android TextWatcher Tutorial with Examples
1. Android TextWatcher
No ADS
As you know, TextEdit allows the user to enter and modify text. TextEdit uses the TextWatcher interface to see changes that have occurred to its text, or to modify the textual content.
editText.addTextChangedListener(TextWatcher watcher)editText.addTextChangedListener(TextWatcher watcher)
Methods of the TextWatcher interface are:
- void afterTextChanged(Editable s)
- void beforeTextChanged(CharSequence s, int start, int count, int after)
- void onTextChanged(CharSequence s, int start, int before, int count)
TextWatcher can be used to ensure that the user enters text that matches a given pattern.
3. Example: Date Pattern
No ADS
In this example, we are going to use the TextWatcher to make sure that the user enters a valid date in the format of DD/MM/YYYY.
DateFormatTextWatcher.java
package org.o7planning.textwatcherdateexample;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import java.util.Calendar;
public class DateFormatTextWatcher implements TextWatcher {
private static final String DDMMYYYY = "DDMMYYYY";
private static final String SEPARATOR = "/";
private final Calendar calendar = Calendar.getInstance();
private String currentText = "";
private EditText editText;
public DateFormatTextWatcher(EditText editText) {
this.editText = editText;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals(this.currentText)) {
// Remove all non-digit.
String newTextClean = s.toString().replaceAll("[^\\d.]|\\.", "");
String currentTextClean = this.currentText.replaceAll("[^\\d.]|\\.", "");
int newTextLength = newTextClean.length();
// Cursor Position Index.
int selectionIndex = newTextLength;
for (int i = 2; i <= newTextLength && i < 6; i += 2) {
selectionIndex++;
}
// Fix for pressing delete next to a forward slash
if (newTextClean.equals(currentTextClean)) {
selectionIndex--;
}
if (newTextClean.length() < 8) {
newTextClean = newTextClean + this.DDMMYYYY.substring(newTextClean.length());
} else {
// This part makes sure that when we finish entering numbers
// the date is correct, fixing it otherwise
int day = Integer.parseInt(newTextClean.substring(0,2));
int month = Integer.parseInt(newTextClean.substring(2,4));
int year = Integer.parseInt(newTextClean.substring(4,8));
month = month < 1 ? 1 : month > 12 ? 12 : month;
this.calendar.set(Calendar.MONTH, month-1);
year = (year < 1900)? 1900:(year > 2100)? 2100 : year;
this.calendar.set(Calendar.YEAR, year);
// ^ first set year for the line below to work correctly
// with leap years - otherwise, date e.g. 29/02/2012
// would be automatically corrected to 28/02/2012
day = (day > this.calendar.getActualMaximum(Calendar.DATE))? this.calendar.getActualMaximum(Calendar.DATE):day;
newTextClean = String.format("%02d%02d%02d",day, month, year);
}
// "%s/%s/%s"
String format = "%s" + SEPARATOR + "%s" + SEPARATOR +"%s";
newTextClean = String.format(format, newTextClean.substring(0, 2),
newTextClean.substring(2, 4),
newTextClean.substring(4, 8));
selectionIndex = selectionIndex < 0 ? 0 : selectionIndex;
this.currentText = newTextClean;
this.editText.setText(this.currentText);
this.editText.setSelection(selectionIndex < this.currentText.length() ? selectionIndex : this.currentText.length());
}
}
@Override
public void afterTextChanged(Editable s) {
}
}
Here is the application interface:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView71"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="Birthday:"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText_birthDay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:ems="10"
android:hint="DD/MM/YYYY"
android:inputType="date"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView71" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package org.o7planning.textwatcherdateexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextWatcher;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.editText = (EditText) this.findViewById(R.id.editText_birthDay);
// Create TextWatcher:
TextWatcher textWatcher = new DateFormatTextWatcher(this.editText);
this.editText.addTextChangedListener(textWatcher);
}
}
4. Example: Number
No ADS
NumberTextWatcher.java
package org.o7planning.textwatchernumberexample;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.util.Log;
import android.widget.EditText;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.Locale;
public class NumberTextWatcher implements TextWatcher {
private static final String LOG_TAG = "AndroidExample";
private final int numDecimals;
private String groupingSeparator;
private String decimalSeparator;
private boolean nonUsFormat;
private DecimalFormat decimalFormatDec;
private DecimalFormat decimalFormatInt;
private boolean hasFractionalPart;
private EditText editText;
private String value;
public NumberTextWatcher(EditText editText, Locale locale, int numDecimals) {
this.editText = editText;
this.numDecimals = numDecimals;
this.hasFractionalPart = false;
this.editText.setKeyListener(DigitsKeyListener.getInstance("0123456789.,"));
DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
char gs = symbols.getGroupingSeparator();
char ds = symbols.getDecimalSeparator();
this.groupingSeparator = String.valueOf(gs);
this.decimalSeparator = String.valueOf(ds);
String patternInt = "#,###";
this.decimalFormatInt = new DecimalFormat(patternInt, symbols);
String patternDec = patternInt + "." + replicate('#', this.numDecimals);
this.decimalFormatDec = new DecimalFormat(patternDec, symbols);
this.decimalFormatDec.setDecimalSeparatorAlwaysShown(true);
this.decimalFormatDec.setRoundingMode(RoundingMode.DOWN);
this.nonUsFormat = !this.decimalSeparator.equals(".");
this.value = null;
}
@Override
public void afterTextChanged(Editable s) {
Log.d(LOG_TAG, "afterTextChanged");
this.editText.removeTextChangedListener(this);
try {
int initLeng = this.editText.getText().length();
String v = this.value.replace(this.groupingSeparator, "");
Number n = this.decimalFormatDec.parse(v);
int selectionStart = this.editText.getSelectionStart();
if (this.hasFractionalPart) {
int decPos = v.indexOf(this.decimalSeparator) + 1;
int decLen = v.length() - decPos;
if (decLen > this.numDecimals) {
v = v.substring(0, decPos + this.numDecimals);
}
int trz = countTrailingZeros(v);
StringBuilder fmt = new StringBuilder(this.decimalFormatDec.format(n));
while (trz-- > 0) {
fmt.append("0");
}
this.editText.setText(fmt.toString());
} else {
this.editText.setText(this.decimalFormatInt.format(n));
}
int endLeng = this.editText.getText().length();
int selection = (selectionStart + (endLeng - initLeng));
if (selection > 0 && selection <= this.editText.getText().length()) {
this.editText.setSelection(selection);
} else {
// Place cursor at the end?
this.editText.setSelection(this.editText.getText().length() - 1);
}
} catch (NumberFormatException | ParseException nfe) {
// Do nothing?
}
this.editText.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d(LOG_TAG, "beforeTextChanged");
this.value = this.editText.getText().toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(LOG_TAG, "onTextChanged");
String newValue = s.toString();
String change = newValue.substring(start, start + count);
String prefix = this.value.substring(0, start);
String suffix = this.value.substring(start + before);
if (".".equals(change) && this.nonUsFormat) {
change = this.decimalSeparator;
}
this.value = prefix + change + suffix;
this.hasFractionalPart = this.value.contains(this.decimalSeparator);
Log.d(LOG_TAG, "VALUE: " + this.value);
}
private int countTrailingZeros(String str) {
int count = 0;
for (int i = str.length() - 1; i >= 0; i--) {
char ch = str.charAt(i);
if ('0' == ch) {
count++;
} else {
break;
}
}
return count;
}
private String replicate(char ch, int n) {
return new String(new char[n]).replace("\0", "" + ch);
}
}
Here is the application interface:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView71"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:layout_marginRight="16dp"
android:text="Enter a Number:"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText_number"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:ems="10"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView71" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package org.o7planning.textwatchernumberexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextWatcher;
import android.widget.EditText;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private EditText editTextNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.editTextNumber = (EditText) this.findViewById(R.id.editText_number) ;
Locale locale = new Locale("en", "US");
int numDecs = 2; // Let's use 2 decimals
TextWatcher textWatcher = new NumberTextWatcher(this.editTextNumber, locale, numDecs);
this.editTextNumber.addTextChangedListener(textWatcher);
}
}
No ADS
Android Programming Tutorials
- Format Credit Card Number with Android TextWatcher
- Android Networking Tutorial with Examples
- Android TextInputLayout Tutorial with Examples
- Create a simple File Finder Dialog in Android
- Android AutoCompleteTextView and MultiAutoCompleteTextView Tutorial with Examples
- Using image assets and icon assets of Android Studio
- Android AsyncTaskLoader Tutorial with Examples
- Android ImageSwitcher Tutorial with Examples
- Android TimePickerDialog Tutorial with Examples
- Android FrameLayout Tutorial with Examples
- Android TimePicker Tutorial with Examples
- How to disable the permissions already granted to the Android application?
- Android ContextMenu Tutorial with Examples
- Android Camera Tutorial with Examples
- Get Phone Number in Android using TelephonyManager
- Android Clipboard Tutorial with Examples
- Android DatePickerDialog Tutorial with Examples
- Android SMS Tutorial with Examples
- Android Space Tutorial with Examples
- Android ScrollView and HorizontalScrollView Tutorial with Examples
- Install Android Studio on Windows
- Android VideoView Tutorial with Examples
- Android ProgressBar Tutorial with Examples
- Android External Storage Tutorial with Examples
- Android 2D Game Tutorial for Beginners
- Enable USB Debugging on Android Device
- Android MediaPlayer Tutorial with Examples
- Android Phone Call Tutorial with Examples
- Example of an explicit Android Intent, calling another Intent
- Android TextWatcher Tutorial with Examples
- Configure Android Emulator in Android Studio
- Android PopupMenu Tutorial with Examples
- Android CharacterPickerDialog Tutorial with Examples
- Android ListView with Checkbox using ArrayAdapter
- Android RadioGroup and RadioButton Tutorial with Examples
- Android AsyncTask Tutorial with Examples
- Android SnackBar Tutorial with Examples
- Android Spinner Tutorial with Examples
- Android ListView Tutorial with Examples
- Android TextView Tutorial with Examples
- Android SeekBar Tutorial with Examples
- Android JSON Parser Tutorial with Examples
- How to know the phone number of Android Emulator and change it
- Android LinearLayout Tutorial with Examples
- Android ViewPager2 Tutorial with Examples
- Android Text to Speech Tutorial with Examples
- Android RatingBar Tutorial with Examples
- Android DialogFragment Tutorial with Examples
- Android Wifi Scanning Tutorial with Examples
- Android Fragments Tutorial with Examples
- Create a simple File Chooser in Android
- Android DatePicker Tutorial with Examples
- Example of implicit Android Intent, open a URL, send an email
- Using Android Device File Explorer
- Android ToggleButton Tutorial with Examples
- Google Maps Android API Tutorial with Examples
- How to add external libraries to Android Project in Android Studio?
- Android EditText Tutorial with Examples
- Android ImageButton Tutorial with Examples
- Android Notifications Tutorial with Examples
- Android CheckBox Tutorial with Examples
- Android Services Tutorial with Examples
- Android Dialog Tutorial with Examples
- Android Tutorial for Beginners - Basic examples
- Create a custom Android Toast
- Android Intents Tutorial with Examples
- Android WebView Tutorial with Examples
- Android Chip and ChipGroup Tutorial with Examples
- Android GridView Tutorial with Examples
- Android Switch Tutorial with Examples
- How to remove applications from Android Emulator?
- Android QuickContactBadge Tutorial with Examples
- Android Chronometer Tutorial with Examples
- Android OptionMenu Tutorial with Examples
- Install Intel® HAXM for Android Studio
- Playing Sound effects in Android with SoundPool
- Android Internal Storage Tutorial with Examples
- Android AlertDialog Tutorial with Examples
- Android SharedPreferences Tutorial with Examples
- ChipGroup and Chip Entry Example
- Android SQLite Database Tutorial with Examples
- Android Toast Tutorial with Examples
- Android TableLayout Tutorial with Examples
- Android Button Tutorial with Examples
- Android FloatingActionButton Tutorial with Examples
- What is needed to get started with Android?
- Android UI Layouts Tutorial with Examples
- Android CardView Tutorial with Examples
- Setting SD Card for Android Emulator
- Android ImageView Tutorial with Examples
- Android Tutorial for Beginners - Hello Android
- Android StackView Tutorial with Examples
- Android TextClock Tutorial with Examples
Show More