Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Styling inline html link in TextView

Tags:

android

I have a TextView with some html text containing a link. I added a style to the TextView and now the link is not visible as a link.

The string:

<string name="strPrivacyPolicyAndContact">See our %1$s Privacy Policy %2$s or contact us for more information: [email protected].</string>

The TextView xml:

<TextView
    android:id="@+id/privacyAndContact"
    style="@style/NewFont_Label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:gravity="left"
    android:text="@string/strPrivacyPolicyAndContact" />

The NewFont_Label style:

<style name="NewFont_Label">
    <item name="android:textSize">12sp</item>
    <item name="variant">medium</item>
    <item name="android:textColor">@color/label</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:letterSpacing">0.02</item>
</style>

The code:

private static final String PRIVACY_HREF = "<a href=\"http://www.beatrixkiddo.com\privacy\">";
private static final String END_TAG = "</a>";

privacyAndContact = (TextView) view.findViewById(R.id.privacyAndContact);
privacyAndContact.setText(Html.fromHtml(getString(R.string.strPrivacyPolicyAndContact, PRIVACY_HREF, END_TAG)));

Before adding the style:

enter image description here

After adding the style:

enter image description here

The question:

I've tried adding all possible html tags with no result.
How do I style the link with an underline or a color?

Update

I've tried SpannableString, but it's impact is being annulled by the applying of the style on the TextView:

SpannableString privacy = new SpannableString(getString(R.string.strPrivacyPolicyAndContact));
privacy.setSpan(new URLSpan("http://www.beatrixkiddo.com/privacy"), 13, 26, 0);
privacy.setSpan(new UnderlineSpan(), 13, 26, 0);
privacy.setSpan(new ForegroundColorSpan(Color.RED), 13, 26, 0);
privacyAndContact.setText(privacy);

So the spannable doesn't work in this case either.

like image 821
Ambran Avatar asked Apr 20 '26 23:04

Ambran


1 Answers

change this two lines only.

private static final String PRIVACY_HREF = "<u><b><font color='green' ><a href='http://www.beatrixkiddo.com/privacy' >Privacy Policy";
private static final String END_TAG = "</a></font></b></u>";
textView.setText(Html.fromHtml(PRIVACY_HREF+END_TAG)); 
like image 60
Vasant Avatar answered Apr 23 '26 12:04

Vasant