Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can achieve this by creating a custom shape drawable resource file and setting it as the background of the EditText.

  1. Create a new drawable resource file in your project (ex. custom_edittext.xml)
  2. Paste the following code into the file:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<!-- Remove the default border -->
<solid android:color="@android:color/transparent" />
<stroke android:width="0dp" />

<!-- Add the underline -->
<stroke android:width="2dp"
    android:color="@color/colorAccent" />

</shape>
  1. In your layout XML file, set the background of the EditText to the drawable resource file you just created:
<EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_edittext" />
  1. Adjust the attributes in the shape drawable file to customize the appearance of the underline as desired.