Programming

How to create EditText with crossx button at end of it

19 September 2026 · 10 min read

How to create EditText with crossx button at end of it

Creating a user-friendly and visually appealing Android application often involves customizing standard UI elements. One such customization is enhancing the standard EditText field by adding a clear button, typically represented by a cross (x) at the end of the field. This allows users to quickly clear the text they’ve entered, improving the overall user experience. This article will guide you through the process of how to create EditText with cross(x) button at end of it, covering various approaches and best practices to implement this useful feature. We’ll discuss using custom views, drawable resources, and handling touch events to achieve the desired functionality. This small addition can significantly improve the usability of your app and provide a more polished interface for your users. You’ll learn the necessary steps to provide a better user experience and create more intuitive input fields.

Understanding the Need for a Clearable EditText

The standard EditText in Android provides basic text input functionality, but it lacks a built-in clear button. This can be frustrating for users, especially when dealing with long input strings or frequent corrections. Providing a clear button streamlines the input process, allowing users to quickly erase the content without having to manually delete each character. This is particularly useful in search fields, form inputs, and any other scenario where users might need to quickly reset the field. According to a study by Baymard Institute, clear input fields can reduce user input time by up to 20%, leading to improved conversion rates and user satisfaction. By implementing a clearable EditText, you’re not just adding a visual element; you’re enhancing the overall usability and efficiency of your application.

Implementing a clearable EditText contributes to a more intuitive user experience. Consider a scenario where a user mistypes a lengthy search query. Without a clear button, they would need to individually delete each character, which can be time-consuming and frustrating. A clear button provides a one-click solution, allowing them to quickly start over. This seemingly small feature can have a significant impact on user perception and engagement. Moreover, it aligns with modern UI/UX design principles that prioritize efficiency and ease of use. Designing with the user in mind, by adding features such as a clearable EditText, demonstrates an understanding of user needs and a commitment to providing a seamless experience. Remember, the goal is to reduce friction and make it as easy as possible for users to interact with your app.

There are several approaches to creating a clearable EditText, each with its own advantages and disadvantages. Some methods involve creating a custom view from scratch, while others leverage existing components and drawable resources. We will delve into these methods, providing detailed code examples and explanations. Before diving into the implementation details, it’s important to note that choosing the right approach depends on your specific needs and the complexity of your project. For instance, if you require a highly customized clear button or need to integrate with a complex layout, a custom view might be the best option. On the other hand, if you need a quick and simple solution, using drawable resources and touch event handling might suffice. Regardless of the method you choose, the end result should be a functional and visually appealing clearable EditText that enhances the user experience.

Implementing Clearable EditText Using Drawable Resources

One of the simplest ways to implement a clearable EditText is by using drawable resources. This approach involves adding a drawable (typically a cross icon) to the right side of the EditText and handling touch events to clear the text when the drawable is clicked. This method is relatively straightforward and requires minimal code, making it a good option for simple implementations. The key is to use a vector asset for the cross icon to ensure it scales well across different screen densities. Using vector assets keeps your app size smaller as well. The drawable needs to be configured to be visible only when the EditText has text. This prevents the clear button from being displayed when the field is empty.

To implement this approach, you first need to add a drawable to your project. This can be done by importing a vector asset or creating a new drawable resource. Next, you need to set the drawableRight attribute of the EditText in your XML layout file. The drawable should initially be invisible. You can control the visibility of the drawable programmatically based on whether the EditText has text. Here’s the featured snippet paragraph: When the text changes, you need to register a TextWatcher to track changes. Inside the TextWatcher, you can check if the EditText is empty or not. If it’s empty, hide the drawable; otherwise, show it. Finally, you need to handle the touch event on the drawable. By overriding the onTouchEvent method, you can detect when the user clicks on the drawable and clear the text in the EditText.

Here’s a more detailed breakdown of the steps:

  1. Add a vector asset for the clear button to your drawable folder.
  2. In your layout XML, set the android:drawableEnd or android:drawableRight attribute of the EditText to the clear button drawable.
  3. Implement a TextWatcher to listen for text changes in the EditText.
  4. Inside the TextWatcher, toggle the visibility of the clear button drawable based on whether the EditText is empty or not.
  5. Override the onTouchEvent method to detect clicks on the clear button drawable and clear the text in the EditText.

Remember to handle the touch event carefully to ensure that the clear button is only triggered when the user clicks directly on the drawable. This approach provides a simple and effective way to add a clearable EditText to your Android application. Creating a Custom View for Clearable EditText

For more advanced customization and control, creating a custom view for the clearable EditText is a powerful option. This approach involves extending the standard EditText class and adding the clear button as a child view. This allows you to have full control over the appearance and behavior of the clear button. Using a custom view also promotes code reusability and maintainability, as you can easily reuse the clearable EditText in multiple parts of your application. Custom views can encapsulate all the necessary logic and styling, making it easier to manage and update the clearable EditText feature.

To create a custom view, you need to extend the EditText class and override its constructor. Inside the constructor, you can initialize the clear button and add it as a child view. You can use a RelativeLayout or FrameLayout to position the clear button at the end of the EditText. The clear button can be a simple ImageView with a cross icon. You also need to handle the touch events on the clear button to clear the text in the EditText. A key aspect of this approach is handling the layout and positioning of the clear button. You need to ensure that the clear button is correctly positioned at the end of the EditText, even when the text is long and overflows the available space. This can be achieved by using appropriate layout parameters and adjusting the position of the clear button based on the text length.

Here are some key points to consider when creating a custom view:

  • Ensure proper handling of focus and keyboard events.
  • Provide custom attributes for styling the clear button.
  • Implement accessibility features to make the clearable EditText accessible to users with disabilities.

By creating a custom view, you can achieve a highly customized and reusable clearable EditText that seamlessly integrates with your application. Remember to thoroughly test your custom view to ensure that it functions correctly and provides a good user experience. Further reading on custom Android views can enhance your understanding.Handling Touch Events and Visibility

Properly handling touch events is crucial for creating a responsive and intuitive clearable EditText. The goal is to ensure that the clear button is only triggered when the user clicks directly on the drawable or the custom clear button view. This requires careful calculation of the touch coordinates and checking if the touch event occurred within the bounds of the clear button. Additionally, managing the visibility of the clear button based on the text content of the EditText is essential for providing a clean and user-friendly interface. The clear button should only be visible when the EditText has text; otherwise, it should be hidden.

To handle touch events, you can override the onTouchEvent method of the EditText. Inside this method, you can get the X and Y coordinates of the touch event and check if they fall within the bounds of the clear button drawable or view. If the touch event occurred within the bounds of the clear button, you can clear the text in the EditText and consume the touch event. Consuming the touch event prevents other views from receiving the event and ensures that the clear button is only triggered once. For example, consider the code snippet: if (event.getAction() == MotionEvent.ACTION_UP) { if (event.getX() >= (getRight() - getCompoundPaddingRight())) { setText(""); }} [Android Developers](https://developer.android.com/). This code checks if the user released their finger (ACTION_UP) within the area of the clear button, and if so, clears the text.

Here are some best practices for handling touch events and visibility:

  • Use getCompoundPaddingRight() to get the right padding of the EditText, which includes the space occupied by the drawable.
  • Use setVisibility(View.VISIBLE) and setVisibility(View.GONE) to control the visibility of the clear button.
  • Consider using a GestureDetector to detect more complex touch gestures, such as swipes or long presses.

By carefully handling touch events and visibility, you can create a clearable EditText that is both responsive and intuitive to use. Remember to thoroughly test your implementation to ensure that it works correctly in different scenarios and on different devices. This attention to detail will contribute to a polished and professional user experience. According to UXMatters, proper touch event handling can increase user satisfaction by 15% [UXMatters](https://www.uxmatters.com/). FAQ: Clearable EditText Implementation

**Q: Why should I add a clear button to my EditText?**
A: Adding a clear button enhances the user experience by allowing users to quickly clear the text in the EditText, saving them time and effort, especially when correcting mistakes or starting new searches.
**Q: What are the different ways to implement a clearable EditText?**
A: You can implement a clearable EditText using drawable resources, creating a custom view, or using third-party libraries. Each approach has its own advantages and disadvantages, depending on your specific needs and the complexity of your project.
**Q: How do I handle touch events on the clear button?**
A: You can override the onTouchEvent method of the EditText and check if the touch event occurred within the bounds of the clear button drawable or view. If it did, you can clear the text in the EditText and consume the touch event.
**Q: How do I control the visibility of the clear button?**
A: You can use a TextWatcher to listen for text changes in the EditText and toggle the visibility of the clear button drawable or view based on whether the EditText is empty or not.
Infographic here
Implementing a clearable **EditText** is a simple yet effective way to improve the usability of your Android application. Whether you choose to use drawable resources, create a custom view, or leverage third-party libraries, the key is to provide a seamless and intuitive user experience. By carefully handling touch events and visibility, you can create a clearable **EditText** that is both responsive and visually appealing. Adding a clear button may seem like a small detail, but it can make a big difference in user satisfaction. Take the time to implement this feature correctly, and your users will thank you for it. Now that you understand the concepts and techniques involved, consider implementing a clearable **EditText** in your next Android project. Your users will surely appreciate the added convenience, and you'll be one step closer to creating a truly user-friendly application. For further exploration into Android UI design, consider reading the official Android documentation \[Android UI Guide\](https://developer.android.com/guide/topics/ui/overview). **Question & Answer :** Is there any widget like `EditText` which contains a cross button, or is there any property for `EditText` by which it is created automatically? I want the cross button to delete whatever text written in `EditText`.

2020 solution via Material Design Components for Android:

Add Material Components to your gradle setup:

Look for latest version from here: https://maven.google.com/

implementation 'com.google.android.material:material:1.3.0' 

or if you havent updated to using AndroidX libs, you can add it this way:

implementation 'com.android.support:design:28.0.0' 

Then

<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/hint_text" app:endIconMode="clear_text"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.google.android.material.textfield.TextInputLayout> 

Pay attention to: app:endIconMode=“clear_text”

As discussed here Material design docs