android on Text Change Listener
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Android's TextWatcher interface lets developers respond to text changes in EditText fields in real time. This is essential for features like search-as-you-type, input validation, character counters, and dynamic UI updates. This article explains how to implement TextWatcher, demonstrates practical examples in both Java and Kotlin, and covers best practices for avoiding common issues.
The TextWatcher Interface
The TextWatcher interface provides three callback methods that fire at different stages of a text modification:
beforeTextChanged(CharSequence s, int start, int count, int after): Called before the text is modified. The parameters tell you what the current text is, where the change starts, how many characters will be removed, and how many will be added.onTextChanged(CharSequence s, int start, int before, int count): Called during the change. Thesparameter contains the new text,startis where the change begins,beforeis how many old characters were replaced, andcountis how many new characters were inserted.afterTextChanged(Editable s): Called after the change is complete. TheEditableparameter lets you modify the text further, which is useful for formatting or validation.
Basic Implementation in Java
Kotlin Implementation with Extension Function
Kotlin makes TextWatcher more concise. You can create an extension function that only requires the callback you care about:
Practical Example: Input Validation with Error Display
A common use case is validating email input and showing an error message in real time:
Practical Example: Debounced Search
Firing a network request on every keystroke wastes resources. Use a Handler to debounce the input so the search only triggers after the user stops typing for a short period:
This waits 300 milliseconds after the last keystroke before executing the search. Each new keystroke cancels the previous pending search.
Practical Example: Character Counter
Common Pitfalls
Infinite loop from modifying text in afterTextChanged. If you change the Editable inside afterTextChanged, the TextWatcher fires again. Guard against this with a flag:
Memory leaks from not removing the listener. In Fragment-based architectures, add the listener in onViewCreated and consider nullifying references in onDestroyView. If you hold a reference to the TextWatcher instance, you can call removeTextChangedListener() during cleanup.
Performing heavy work on the main thread. Network calls, database queries, and complex computations inside onTextChanged or afterTextChanged will freeze the UI. Always offload heavy work to a coroutine or background thread, and use debouncing to limit invocation frequency.
Using onTextChanged instead of afterTextChanged for validation. The onTextChanged method fires before the Editable is finalized. If you read the text at that point, cursor positioning and formatting may not be complete. Prefer afterTextChanged for any logic that depends on the final state of the input.
Summary
The TextWatcher interface is the standard way to observe text changes in Android EditText fields. Implement afterTextChanged for validation and search triggers, use debouncing to limit network calls, and guard against infinite loops when modifying text inside the callback. Kotlin extension functions simplify the boilerplate significantly. Always remove listeners when the view is destroyed to prevent memory leaks.
Related reading
- Android "Only the original thread that created a view hierarchy can touch its views.
- Android Paint .measureText vs .getTextBounds
- Android permission doesn't work even if I have declared it
- android pick images from gallery
- Android Preventing Double Click On A Button
- Android Push Notifications Icon not displaying in notification, white square shown instead
- Android RecyclerView addition removal of items
- Android Recyclerview GridLayoutManager column spacing
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.