6

I want to make a TextView with a link. I made it with combination of html and bit of java:

// used to enable link navigation on TextView
setMovementMethod(LinkMovementMethod.getInstance())

// TextView with link
<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="19dp"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:linksClickable="true"
    android:text="@string/link"/>

// @string/link
<string name="link">Test <a href="#">link</a></string>

However there is still one issue, the space before actual link text is underlined like this:

enter image description here

Why is that and how could it be fixed?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
antanas_sepikas
  • 5,644
  • 4
  • 36
  • 67

1 Answers1

3

Use CDATA in string to use HTML tags and use Html.fromHtml() method to set the text.

Implementation below:

Set the text using Html.fromHtml() in your Activity class.

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(Html.fromHtml(getString(R.string.link)));
textView.setMovementMethod(LinkMovementMethod.getInstance());

In strings.xml modify as below:

<string name="link">Test <![CDATA[<a href="#">link</a>]]></string>
Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126