How to set underline text on textview
?
I have used following code but it is not working.
tvHide.setText(Html.fromHtml("
I believe that you need to use CharSequence. You can find an example here.
You can easily add a View with the height of 1dp aligning start and the end of TextView, right below it. You can also use marginTop with negative value to make the underline closer to TextView. If you use your TextView and the under inside a relative layout, it would be much easier for alignments. here is the example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/viewForgotPasswordUnderline"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_below="@id/txtForgotPassword"
android:layout_alignStart="@id/txtForgotPassword"
android:layout_alignEnd="@id/txtForgotPassword"
android:layout_marginTop="-2dp"
android:background="@color/lightGray" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtForgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:paddingTop="8dp"
android:text="@string/userLoginForgotPassword"
android:textColor="@color/lightGray"
android:textSize="16dp" />
</RelativeLayout>
Android supports string formatting using HTML tags in the strings xml. So the following would work:
<string name="label_testinghtmltags"><u>This text has an underscore</u></string>
Then you just use it as you normally would. You can see more in the documentation here
Update:
To further expand on the answer, the documentation says:
... the
format(String, Object...)
andgetString(int, Object...)
methods strip all the style information from the string. . The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place
For example, the resulting string from the following code snippet will not show up as containing underscored elements:
<string name="html_bold_test">This <u>%1$s</u> has an underscore</string>
String result = getString(R.string.html_bold_test, "text")
However the result from the following snippet would contain underscores:
<string name="html_bold_test">This <u> %1$s </u> has an underscore</string>
String result = Html.fromHtml(getString(R.string.html_bold_test, "text"))
Try this,
tv.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
Use this
tvHide.setPaintFlags(tvHide.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
You can do it like:
tvHide.setText(Html.fromHtml("<p><span style='text-decoration: underline'>Hide post</span></p>").toString());
Hope this helps