Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Monitor TextView Changes in Android

DZone's Guide to

How to Monitor TextView Changes in Android

In this tutorial we will see how to monitor the text changes in Android TextView or EditText.

· Mobile Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

Some form based applications needs to continuously monitor for the text changes to perform the front end form validations. This can be achieved by implementing the android.text.TextWatcher interface. The TextWatcher interface provides the following three callback methods, that are called while the textview is being updated.

public void beforeTextChanged(CharSequence s, int start, int count, int after);
public void onTextChanged(CharSequence s, int start, int before, int count);
public void afterTextChanged(Editable s);

The beforeTextChanged() and onTextChanged() methods are provided mainly as notifications, as shouldn’t make changes to the CharSequence in either of these methods. If you are attempting to intercept the text entered into the view, changes may be made when afterTextChanged() is called.

First, you need to register a TextWatcher instance with a TextView, call the textView.addTextChangedListener() method. Notice from the syntax that more than one TextWatcher can be registered with a TextView. Now that we understand the methods to be used, let us examine the sample for the usage.

Count Characters on TextView Changes

A simple use of TextWatcher is to create a live character counter that follows an EditText as the user types or deletes information.

public class MyActivity extends Activity implements TextWatcher {
private EditText text;
private int textCount;
private static final String TAG = "MyActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create an EditText widget and add the watcher
editText = new EditText(this);
editText.addTextChangedListener(this);
setContentView(editText);
}

/* TextWatcher Implementation Methods */
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
textCount = editText.getText().length();
setTitle(String.valueOf(textCount));
Log.e(TAG, String.valueOf(textCount))
}

@Override
public void afterTextChanged(Editable s) {

}
}

Because our needs do not include modifying the text being inserted, we can read the count from onTextChanged(), which happens as soon as the text change occurs. The other methods are unused and left empty.

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:
android

Published at DZone with permission of Nilanchala Panigrahy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}