DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Monitor AI Models Without Drowning in Alerts
  • Pragmatic Paths to On-Device AI on Android with ML Kit
  • Deep Linking in Enterprise Android Apps: A Real-World, Scalable Approach
  • Diving into JNI: My Messy Adventures With C++ in Android

Trending

  • Don't Adopt Anything Announced at re:Invent for 90 Days
  • Architecture Review Boards for Generative AI: A Framework for Governance, Risk Management, and Responsible Adoption
  • What's Really in an Agent's Context Window
  • From Theory to Practice: Building an Enterprise Ontology With Python and Kubernetes

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. How to Monitor TextView Changes in Android

How to Monitor TextView Changes in Android

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

By 
Nilanchala Panigrahy
·
Aug. 07, 15 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Some form-based applications need to continuously monitor for 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 text view 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 and 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 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.

Java
 
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, which happens as soon as the text change occurs. The other methods are unused and left empty.

Monitor (synchronization) Android (robot)

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

Opinions expressed by DZone contributors are their own.

Related

  • How to Monitor AI Models Without Drowning in Alerts
  • Pragmatic Paths to On-Device AI on Android with ML Kit
  • Deep Linking in Enterprise Android Apps: A Real-World, Scalable Approach
  • Diving into JNI: My Messy Adventures With C++ in Android

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook