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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Issue and Present Verifiable Credentials With Spring Boot and Android
  • How to Build a React Native Chat App for Android
  • Using Jetpack Compose With MVI Architecture
  • Implementing SOLID Principles in Android Development

Trending

  • Prioritizing Cloud Security Risks: A Developer's Guide to Tackling Security Debt
  • Implementing API Design First in .NET for Efficient Development, Testing, and CI/CD
  • Rust, WASM, and Edge: Next-Level Performance
  • The Role of AI in Identity and Access Management for Organizations
  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 user avatar
Nilanchala Panigrahy
·
Aug. 07, 15 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
7.7K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Issue and Present Verifiable Credentials With Spring Boot and Android
  • How to Build a React Native Chat App for Android
  • Using Jetpack Compose With MVI Architecture
  • Implementing SOLID Principles in Android Development

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!