21

How to give autolink for some part of textview ? For example : My text inside TextView is "Please click here to open this webpage". I want to show link for only the text "here". And I should open that webpage onclick of the text "here" but not on the anywhere of TextView.

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121

5 Answers5

41

Put a String in string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="txtCredits">Support: <a href="http://www.stackoverflow.com">click here</a></string>
</resources>

And you can use it in textView like this:

<TextView
        android:layout_width="fill_parent"
        android:id="@+id/text"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:linksClickable="true"
        android:text="@string/txtCredits" />

EDIT

For some reason above code does not work properly. So, add below code also,

TextView t2 = (TextView) findViewById(R.id.text);
t2.setMovementMethod(LinkMovementMethod.getInstance());

Important: Don't forget to remove autoLink="web" if you are calling setMovementMethod().

tostao
  • 2,803
  • 4
  • 38
  • 61
asish
  • 4,767
  • 4
  • 29
  • 49
  • 11
    From my testing this doesn't actually make the hyperlink clickable. It styles it correctly (makes it blue with an underline) but you can't actually click it. It seems the only way is here: http://stackoverflow.com/a/2746708/708906 – Tony Chan Nov 23 '12 at 06:56
  • 1
    tested on M, without .setMovementMethod(), i can see the hiperlink text underligned, without any action onClick, if I add .setMovementMethod() i don't see the hiperlink text anymore and of course, there is no action....funny business – DoruChidean Nov 10 '16 at 10:31
  • 2
    Just remove this 2 line, android:autoLink="web" and android:linksClickable="true"....then it will work. – kalandar Feb 13 '17 at 19:00
8

Textviews are capable of displaying HTML, which solves your problem. Wrap what you want clickable in a hyperlink:

String html = "My link is <a href=\"http://google.com\">here</a>";
myTextView.setText(Html.fromHtml(html));
Joakim Berglund
  • 2,859
  • 1
  • 22
  • 30
  • Do i need to set onClickListener for TextView in this case also ? – Rahul Baradia Apr 20 '12 at 12:58
  • 3
    This is about the worst possible approach I've seen, sorry. How's that string look as a resource, in XML? `Linkify`, `ClickableSpan`, `android:autoLink`. Pick your favorite, but don't use HTML like this. – Madbreaks Nov 02 '12 at 00:21
  • 3
    From my testing this doesn't actually make the hyperlink clickable. It renders it correctly (makes it blue with an underline) but you can't actually click it. Setting the xml attributes `linksClickable="true"` and `autoLink="true"` have no effect either. Seems the only way to do this is here: http://stackoverflow.com/a/2746708/708906 – Tony Chan Nov 23 '12 at 06:52
2

use simple Url in strings.xml :

<string name="autolink_val">Please Click Here : http://www.google.com</string>

And in Java code write this:

<TextView android:id="@+id/linkVal"   
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:autoLink="web" 
          android:text="@string/autolink_val1"/>`
Dmytro Rostopira
  • 10,588
  • 4
  • 64
  • 86
Agam Shah
  • 91
  • 1
  • 3
  • 3
    `android:autoLink="web"` doesn't allow for a link that says ["here"](http://www.example.com/); it just allows the url itself to function as a link, as in: [http://www.example.com/](http://www.example.com/). – dokkaebi Dec 21 '12 at 21:39
1

Use HTML syntax in strings.xml:

<string name="test">Click &lt;a href="http://vtuhtan.info"&gt;here&lt;/a&gt;</string>

Set TextView properties to have links clickable and auto link.

TextView tv = findViewById(R.id.textView);
tv.setText(Html.fromHtml(getResources().getString(R.string.test)));
vtuhtan
  • 1,056
  • 7
  • 18
1

You can test it with following code:

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="http://www.yahoo.com"
    android:autoLink="web"
    />
Saleem Kalro
  • 1,046
  • 9
  • 12