How to Hide the Underline in an EditText in Android

How to Hide the Underline in an EditText in Android

6 June 2024 Stephan Petzl Leave a comment Tech-Help

When working with Android development, there are times when you might want to make an EditText appear without the default underline. This can be particularly useful when you want to display text in a read-only manner, while maintaining the same font and height. Here we provide several methods to achieve this effect.

Using XML Attributes

One of the simplest ways to hide the underline is by modifying the background attribute of the EditText in your XML layout file:

  • android:background="@android:color/transparent"
  • android:background="@null"

Example:

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent" />

Programmatically Setting the Background

If you prefer to set the background programmatically, you can do so using the following code:

EditText editText = findViewById(R.id.editText);
editText.setBackgroundResource(android.R.color.transparent);

Using a Custom Drawable

You can also create a custom drawable resource and set it as the background for your EditText. This method allows you to maintain padding and other style attributes:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF" />
</shape>

Set this drawable as the background:

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_background" />

Using TextInputLayout

If you are using TextInputLayout, you can set the boxBackgroundMode attribute to none to remove the underline:

<com.google.android.material.textfield.TextInputLayout
    app:boxBackgroundMode="none"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>

Conclusion

Hiding the underline in an EditText can be achieved in multiple ways depending on your requirements. Whether you choose to use XML attributes, programmatically set the background, or leverage a custom drawable, each method offers a straightforward solution.

Streamlining Mobile App Testing with Repeato

While managing UI elements like EditText is crucial, ensuring that your app functions as expected is equally important. This is where automated testing tools like Repeato come into play. Repeato is a no-code test automation tool for iOS and Android that allows you to create, run, and maintain tests with ease. Leveraging computer vision and AI, Repeato simplifies the testing process, enabling you to focus on developing a great product. Additionally, it allows non-technical team members to participate in the testing process, making it a versatile tool for any development team.

Like this article? there’s more where that came from!