Can underline words in TextView text
Asked Answered
A

5

28

Is there possibility in android to provide TextView some text in Java code with setText(text) function with basic tags like and to make marked words underlined ?

Allan answered 19/12, 2011 at 7:17 Comment(2)
This will help you, this is the example by which you can underline your textview text and also italic.Servant
possible duplicate of How to display HTML in TextView?Alena
J
41

Yes, you can, use the Html.fromhtml() method:

textView.setText(Html.fromHtml("this is <u>underlined</u> text"));
Joniejonina answered 19/12, 2011 at 7:21 Comment(3)
well, I am facing a problem. Here is the line of codes that is not working. textView.setText(Html.fromHtml(getResources().getString(R.string.have_activation_code))); <string name="have_activation_code"><u>I have activation code</u></string>Dysgenic
@mahbub.kuet You have to put your HTML string into <![CDATA[]]>. So <string name="have_activation_code"><![CDATA[<u>I have activation code</u>]]></string>Aphis
Html.fromHtml is deprecated nowBullion
C
34

Define a string as:

<resources>
    <string name="your_string">This is an <u>underline</u> text demo for TextView.</string>
</resources>
Clipfed answered 19/12, 2011 at 7:22 Comment(2)
Yes but you can use it for dynamic strings by using Html.fromHtml(), check above answer https://mcmap.net/q/54749/-can-underline-words-in-textview-textClipfed
@PareshMayani but beware, there are tags not supported by Html.fromHtml(). Check this out https://mcmap.net/q/54747/-html-list-tag-not-working-in-android-textview-what-can-i-doHoroscope
G
10

You can use UnderlineSpan from SpannableString class:

SpannableString content = new SpannableString(<your text>);
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);

Then just use textView.setText(content);

Gittle answered 19/12, 2011 at 7:23 Comment(0)
Z
3
tobeunderlined= <u>some text here which is to be underlined</u> 

textView.setText(Html.fromHtml("some string"+tobeunderlined+"somestring"));
Zoba answered 19/12, 2011 at 8:49 Comment(0)
D
3

Most Easy Way

TextView tv = findViewById(R.id.tv);
tv.setText("some text");
setUnderLineText(tv, "some");

Also support TextView childs like EditText, Button, Checkbox

public void setUnderLineText(TextView tv, String textToUnderLine) {
        String tvt = tv.getText().toString();
        int ofe = tvt.indexOf(textToUnderLine, 0);

        UnderlineSpan underlineSpan = new UnderlineSpan();
        SpannableString wordToSpan = new SpannableString(tv.getText());
        for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
            ofe = tvt.indexOf(textToUnderLine, ofs);
            if (ofe == -1)
                break;
            else {
                wordToSpan.setSpan(underlineSpan, ofe, ofe + textToUnderLine.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
            }
        }
    }

If you want

- Clickable underline text?

- Underline multiple parts of TextView?

Then Check This Answer

Dolorous answered 7/8, 2020 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.