9

I have a few links in my application. One for a website, one for a phone number, and one for an email. The email and phone links are both working and clickable, however the website hyperlink is still not clickable for some reason. Any thoughts? Code below.

<string name="website" ><a href="http://www.XXXXXX.com">XXXXXX Website</a></string>
<string name="email" >sales@XXXXXXX.com</string>
<string name="phone" >P: XXX.XXX.XXXX</string>
<string name="fax" >F: XXX.XXX.XXXX</string>

Above are my strings, and below is the xml file that displays them:

<TextView android:id="@+id/website"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/imageButtonTwitter"
          android:gravity="center_horizontal"
          android:padding="10dp"
          android:autoLink="web"
          android:clickable="true"
          android:linksClickable="true"
          android:text="@string/website" />

<TextView android:id="@+id/email"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/website"
          android:gravity="center_horizontal"
          android:padding="10dp"
          android:autoLink="email"
          android:linksClickable="true"
          android:text="@string/email" />

<TextView android:id="@+id/phone"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/email"
          android:gravity="center_horizontal"
          android:padding="10dp"
          android:autoLink="phone"
          android:linksClickable="true"
          android:text="@string/phone" />

<TextView android:id="@+id/fax"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/phone"
          android:gravity="center_horizontal"
          android:padding="10dp"
          android:text="@string/fax" />

Like I said.. the others are clickable and working. I've tested it on two emulators, as well as my Galaxy S4. Any thoughts why the website is not clickable?

Josh Beckwith
  • 1,432
  • 3
  • 20
  • 38
  • This appears to be the same problem: [http://stackoverflow.com/questions/11413372/textview-hyperlink-is-not-working?rq=1][1] [1]: http://stackoverflow.com/questions/11413372/textview-hyperlink-is-not-working?rq=1 – Jim Jan 03 '14 at 23:35

3 Answers3

38

You need to call this on your textview:

TextView tv = (TextView) findViewById(R.id. website);
tv.setMovementMethod(LinkMovementMethod.getInstance());

You need to remove from your TextView:

android:autoLink="web"
android:clickable="true"
android:linksClickable="true"
Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • 2
    I tried this before and took it out because it didn't work. I tried it again just to make sure and still no dice. Without that java snippet, the hyperlink is at least blue as if it is clickable, now the link is white and non clickable. – Josh Beckwith Jan 03 '14 at 23:43
  • I think you need to remove the various clickable attributes on your textview, thats the only difference between yours and mine. I will udpate answer – Greg Ennis Jan 03 '14 at 23:46
  • 1
    Still no luck. : / Edit: I just had to take out the autoLink attribute and it's good to go! Thanks for the help. Best Answered. – Josh Beckwith Jan 03 '14 at 23:50
  • 2
    Weird: `setMovementMethod(LinkMovementMethod.getInstance())` does NOT work for me, but setting the stuff you told to REMOVE in the xml, does. – mathematics-and-caffeine May 18 '21 at 21:32
  • 1
    @LukasNiessen yes as you said, for me also it worked with the one he told to remove :\. thanks u saved my day – Amrutha Saj Sep 14 '21 at 11:25
  • make sure you write this line tv.setMovementMethod(LinkMovementMethod.getInstance()); after setting the html text to the textView – RAINA Nov 10 '22 at 16:17
1

Add this line -

htmlContent.setMovementMethod(LinkMovementMethod.getInstance());

A J
  • 4,542
  • 5
  • 50
  • 80
0

I'm not very experienced android programmer, but my small remarks which can be helpful (at least I hope):

Don't add those additional parameters to TextView. Just android:text and android:clickable="true" is enough for having html clickable link.

And next when setting text please use:

TextView myTextView = (TextView) findViewById(R.id.myId);
myTextView.setText(Html.fromHtml(htmlText));

More here: android.text.Html

I'm guessing you are missing that Html.fromHtml

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
user2707175
  • 1,133
  • 2
  • 17
  • 44