Open In App

How to Create Dashed Underline under TextView in Android?

Last Updated : 06 Jun, 2021
Like Article

In this article, we will explain how to create a dashed underline below a text view in android. A sample image is shown below to give an idea of what we are going to build.

Step by Step Implementation

Step 1: Create a new project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio

Step 2: Working with dashed_underline.xml

Go to res > drawable > new > drawable resource file and create a new file and name it “dashed_underline.xml” and define all properties of the dashed line that we need. Later we will add this as the background of TextView. Below is the code for dashed_underline.xml comments is added for a better understanding of the code.


<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="" >
<!--Create a layer list and add item of shape "Rectangle to it"-->
            <!--Give the dashed lines their property
                 like color, width, and dashGap-->

Step 3: Working with the activity_main.xml

Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file. It has only a TextView that we want to be underlined.


<?xml version="1.0" encoding="utf-8"?>
    <!--Create a Text View and inside the 
        background tag add the dashed_underline
        that we created earlier-->
        android:text="Welcome to GFG!"
        app:layout_constraintTop_toTopOf="parent" />

Output UI:

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads