Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView anchor link space

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?

like image 683
antanas_sepikas Avatar asked Mar 16 '18 09:03

antanas_sepikas


People also ask

How do I make part of a TextView clickable?

In Android, the most common way to show a text is by TextView element. The whole text in the TextView is easy to make clickable implementing the onClick attribute or by setting an onClickListener to the TextView.

How do I use Linkify?

To call linkify in android, we have to call linkify. addLinks(), in that method we have to pass textview and LinkifyMask. Linkify. WEB_URLS: It going make URL as web url, when user click on it, it going to send url to default web browsers.

What is LinkMovementMethod?

addLinks is used to add links to a TextView, Android uses a class known as LinkMovementMethod for highlighting links when they're focused and dispatching Intents when they're clicked.


1 Answers

// @string/link

<string name="link1">Test&#160;<a href="#">link</a></string>

You can use the white space in xml as string use &#160;. XML won't take white space as it is. it will trim the white space before setting it. So use &#160; instead of single white space.

like image 105
Ashish Das Avatar answered Sep 19 '22 10:09

Ashish Das