user1945196
user1945196

Reputation:

How do I Underline Specific text in a TextView?

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="10dp"
        android:text="Don&apos;t worry we will never share this with anyone\nBy Clicking register you are indicating that you have read and agreed to the \n **Terms of services**  and **Privacy Policy**"
        android:textColor="@android:color/black" 
        android:textSize="8.5sp"
    />

From the above code I need to underline Terms of Services and Privacy Policy which are being highlighted bold.

I had tried using <u></u> but doesn't worked.

Also I need to increase the font of Terms of Service, Privacy Policy only

Please do give some suggestions.

Upvotes: 0

Views: 832

Answers (2)

Nezam
Nezam

Reputation: 4132

You need to define your text in strings.xml file.Yes,you can define formatted html in it too.

So you can try this out:

<string name="nice_html">
<![CDATA[Don&apos;t worry we will never share this with anyone<br/>
By Clicking register you are indicating that you have read and agreed to the <br/>
 <u>Terms of services</u>  and <u>Privacy Policy</u>
]]></string>

Then, in your code:

TextView foo = (TextView)findViewById(R.id.foo); foo.setText(Html.fromHtml(getString(R.string.nice_html)));

Original answer.

This is the cleanest way to do it.Inshallah.

Upvotes: 1

Archie.bpgc
Archie.bpgc

Reputation: 24012

String mString=new String("Don&apos;t worry we will never share this with anyone\nBy Clicking register you are indicating that you have read and agreed to the \n **Terms of services**  and **Privacy Policy**");
SpannableString span = new SpannableString(mString);
span.setSpan(new UnderlineSpan(), index of T in terms, mString.length(), 0);
mTextView.setText(span);

Upvotes: 0

Related Questions