If your TextView has fixed width, alternative solution can be to create a View which will look like an underline and position it right below your TextView.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/myTextView"
android:layout_width="20dp"
android:layout_height="wrap_content"/>
<View
android:layout_width="20dp"
android:layout_height="1dp"
android:layout_below="@+id/myTextView"
android:background="#CCCCCC"/>
</RelativeLayout>
In Kotlin you can create extension property:
inline var TextView.underline: Boolean
set(visible) {
paintFlags = if (visible) paintFlags or Paint.UNDERLINE_TEXT_FLAG
else paintFlags and Paint.UNDERLINE_TEXT_FLAG.inv()
}
get() = paintFlags and Paint.UNDERLINE_TEXT_FLAG == Paint.UNDERLINE_TEXT_FLAG
And use:
textView.underline = true
5 Amazing Ways To Underline A TextView In Android - Kotlin/Java & XML
String html = "<u>Underline using Html.fromHtml()</u>";
textview.setText(Html.fromHtml(html));
But Html.fromHtml(String resource) was deprecated in API 24.
So you can use the latest android support library androidx.core.text.HtmlCompat. Before that, you need to include the dependency in your project.
`implementation 'androidx.core:core:1.0.1'`
String html = "<u> 1.1 Underline using HtmlCompat.fromHtml()</u>";
//underline textview using HtmlCompat.fromHtml() method
textview11.setText(HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY));
Using strings.xml,
<string name="underline_text">1.3 <u>Underline using HtmlCompat.fromHtml() and string resource</u></string>
textview13.setText(HtmlCompat.fromHtml(getString(R.string.underline_text), HtmlCompat.FROM_HTML_MODE_LEGACY));
using PaintFlags
textview2.setPaintFlags(textview2.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
textview2.setText("2. Underline using setPaintFlags()");
using SpannableString
`String content1 = "3.1 Underline using SpannableString";
SpannableString spannableString1 = new SpannableString(content1);
spannableString1.setSpan(new UnderlineSpan(), 0, content1.length(), 0);
textview31.setText(spannableString1);`
You can use Kotlin Extension and type your own drawUnderLine
method.
fun TextView.drawUnderLine() {
val text = SpannableString(this.text.toString())
text.setSpan(UnderlineSpan(), 0, text.length, 0)
this.text = text
}
yourTextView.drawUnderLine()
Its works for me.
tv.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
A simple and sustainable solution is to create a layer-list and make it as the background of your TextView:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-5dp"
android:right="-5dp"
android:top="-5dp">
<shape>
<stroke
android:width="1.5dp"
android:color="@color/colorAccent" />
</shape>
</item>
</layer-list>
For anyone still looking at this querstion. This is for a hyperlink but you can modify it for just a plain underline:
Create a drawable (hyperlink_underline.xml):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-10dp"
android:left="-10dp"
android:right="-10dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
<stroke android:width="2dp"
android:color="#3498db"/>
</shape>
</item>
</layer-list>
Create a new style:
<style name="Hyperlink">
<item name="android:textColor">#3498db</item>
<item name="android:background">@drawable/hyperlink_underline</item>
</style>
Then use this style on your TextView:
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
local:MvxBind="Text Id; Click ShowJobInfoCommand"
style="@style/HyperLink"/>
just surround your text with < u > tag in your string.xml resource file
<string name="your_string"><u>Underlined text</u></string>
and in your Activity/Fragment
mTextView.setText(R.string.your_string);
Source: Stackoverflow.com