If you’re coming from web development, you might be looking for something similar to HTML/JavaScript’s onChange event for Android’s EditText widget. Here’s how to implement real-time change detection.

Common Use Cases

Monitoring EditText changes is useful for:

  1. Character count display - Showing users how many characters they’ve typed
  2. Remaining characters - Like Twitter’s character limit countdown
  3. Live processing - Sending partial input to a server for autocomplete suggestions

The Solution: TextWatcher

Android provides the TextWatcher interface for monitoring text changes. Register it with your EditText using addTextChangedListener():

EditText editText = findViewById(R.id.et_text);
TextView charCount = findViewById(R.id.char_count);

editText.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {
        // Called after the text has changed
        charCount.setText(String.format("%d characters", s.length()));
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // Called before the text is changed
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // Called as the text is being changed
    }
});

Which Callback to Use?

  • beforeTextChanged - Called before changes are applied. Useful for capturing the previous state.
  • onTextChanged - Called during the change. You get info about what’s being added/removed.
  • afterTextChanged - Called after changes are complete. Best for most use cases like updating UI.

For simple scenarios like character counting, afterTextChanged is usually what you need. The Editable parameter gives you direct access to the current text content.