Sometimes we want to add some html like styles in our text. There are 3 different approach to do it:
To make text underline, put below given line of code in onCreate method of your activity:
We can use setPaintFlags method to make a text underline:
We can use Html.fromHtml(htmlString) to add html like styles:
It can be achieved if we are using a string resource xml file, which supports HTML tags like <b></b>, <i></i> and <u></u>.
In this example, I have tried to demonstrate all 4 approach to underline a text. Several html tags are supported if you want to use approach 3 and 4.
Here is the result screen of this example
Result of sample
Step 1: Create a new project in Android Studio and name it Underline text in android.
Step 2: Open res -> layout -> activity_main.xml and add following code
Step 3: Open your MainActivity.java and onCreate method set your desired font for view. Source of my MainActivity.java is given below:
Step 4: Open res -> values ->strings.xml and replace it with below given code.
Step 5: Clean and run the project to see the result.