How to create EditText with crossx button at end of it?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
An EditText with an X button at the end is usually called a clearable text field. The classic Android implementation uses a compound drawable plus touch handling, while modern Material-based apps can often use TextInputLayout end icons instead.
The important design choice is whether you want a quick custom EditText solution or a cleaner Material Components solution. Both work, but the modern option usually requires less manual touch math.
Classic EditText With a Drawable End Icon
Start with a normal EditText in XML and give it an end drawable:
That only shows the icon. It does not make it clear the text when tapped.
Handle the Tap in Code
The usual approach is to listen for touch events and check whether the user tapped inside the drawable area.
Kotlin example:
This works by checking whether the touch landed inside the end-drawable region.
Show the X Only When There Is Text
Most apps hide the clear icon when the field is empty. That behavior feels more natural and reduces visual clutter.
Now the field only shows the clear button when there is something to clear.
A Reusable Custom View
If you need this behavior in many screens, a custom subclass is cleaner than repeating touch and watcher logic everywhere.
That keeps the behavior localized and reusable.
Modern Material Alternative
If you are already using Material Components, TextInputLayout often gives a cleaner solution with less custom code.
This is usually the best modern answer if your project already depends on Material Components. It gives you the clear-text behavior without manual touch calculations.
Which Approach Should You Use
Use the classic drawable-and-touch approach when:
- the app already uses plain
EditText - you need full custom control
- or you do not want to add Material Components
Use TextInputLayout when:
- the app already uses Material
- you want a built-in clear-text pattern
- and you prefer less custom event code
For new apps, the Material approach is often more maintainable.
Common Pitfalls
One common mistake is showing the X icon permanently even when the field is empty. That usually looks unfinished and wastes touchable space.
Another mistake is calculating the touch area incorrectly, especially in right-to-left layouts. Using relative compound drawables helps with that.
It is also easy to forget accessibility. A clear button should have predictable behavior and should not interfere with normal text selection or editing interactions.
Finally, some teams reimplement this pattern manually even though TextInputLayout already provides it. If Material Components are already in use, the built-in solution is usually better.
Summary
- A clearable
EditTextusually uses an end drawable plus touch handling. - Show the
Xonly when the field actually contains text. - Wrap the behavior in a custom view if you need it in several places.
- In modern Material apps,
TextInputLayoutwithendIconMode="clear_text"is often the cleanest solution. - Choose the approach that matches the UI toolkit already used by the app.
Related reading
- How to create EditText with rounded corners?
- How to create empty constructor for data class in Kotlin Android
- How to create fixed space and flexible space bar button items programmatically?
- How to create .ipa file using Xcode?
- How to create IPA in Xcode 6?
- How to create NS_OPTIONS-style bitmask enumerations in Swift?
- How to create NS_OPTIONS-style bitmask enumerations in Swift?
- How to create NSIndexPath for TableView
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.