22

I create a TextView dynamically and want to set the text as linkable. Text value is "Google". I referred to internet & blogs like this, which shows the same way, but I couldn't produce the expected results.

I tried different ways, but the output I see is the whole text with text only. The code I have tried with is :

TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
// Make Linkable
tv1.setMovementMethod(LinkMovementMethod.getInstance());
tv1.setText(Html.fromHtml(l.getLeftString()));

/*SpannableString s = new SpannableString(l.getLeftString());
Linkify.addLinks(s, Linkify.WEB_URLS);
tv1.setText(s);                 
tv1.setMovementMethod(LinkMovementMethod.getInstance());
*/
dialogLayout.addView(tv1);

In my output I see "Google" and no link. I also tried Clean project & building it again, but no success. I am looking to see only "Google" as underlined with blue color (as default) and on clicking Google, the browser open with http://google.com.

What is lacking in my code to get the output ? BTW For REF : I use 64bit Win 7, Java, Eclipse, Android API 8-2.2

Any help is highly appreciated.

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
Tvd
  • 4,463
  • 18
  • 79
  • 125
  • 1
    Look at http://stackoverflow.com/questions/12069811/android-hyperlinks-on-textview-in-custom-alertdialog-not-clickable/12070751#12070751 – user370305 Aug 25 '12 at 06:39
  • @user370305, removed Html.fromHtml, Tried with & without setClickable, but no success - still see the full text – Tvd Aug 25 '12 at 06:56

6 Answers6

49

I finally got it working using the following code :

TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
tv1.setText(Html.fromHtml("<a href=\""+ l.getRightString() + "\">" + l.getLeftString() + "</a>"));
tv1.setClickable(true);
tv1.setMovementMethod (LinkMovementMethod.getInstance());
dialogLayout.addView(tv1);

l.getRightString() - contains a url like http:\www.google.com l.getLeftString() - contains text for the url like "Go to Google"

RESULTS : Text "Go to Google" on my dialog with blue color and underlined, and on clicking it the browser opens up and shwows the respective page. On returning/Exiting the browser it again comes to the app from the state where it had left.

Hope this helps.

Tvd
  • 4,463
  • 18
  • 79
  • 125
  • 1
    In my case, the setClickable(true) is not needed. But if I try to add the link with an append() to another text, it does not work :S – AlvaroSantisteban Aug 15 '13 at 12:52
  • 18
    Important. Do not set android:autoLink="web" because with that it doesn't work! – Gelldur Mar 31 '15 at 14:22
  • 4
    @DawidDrozd that is very important indeed. Thanks mate, you saved me from killing myself :) – Rakeeb Rajbhandari Aug 18 '15 at 07:00
  • I read and tried many other solutions; this is the only one that worked for me in following map links. btw, use urlEncoder to encode your addresses - it works like a charm. – Opus1217 Apr 23 '16 at 03:38
  • As @DawidDrozd mentioned you should not combine setMovementMethod with android:autoLink="web" (or any value), but as an extra note: I wasn't using the autoLink property but Linkify.addLinks(spannableString, Linkify.ALL) instead. This is the same, so don't combine those either! – Kenneth Saey Aug 02 '18 at 14:03
  • Of all the answers on this site yours is the perfect one. – Melaku Minas Kasaye Jan 15 '21 at 23:32
13

Save your html in a string

<string name="link">&lt;a href="http://www.google.com">Google&lt;/a></string>

Set textview ID to to

textViewLinkable

In main activity use following code:

((TextView) findViewById(R.id.textViewLinkable)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.textViewLinkable)).setText(Html.fromHtml(getResources().getString(R.string.link)));
SquiresSquire
  • 2,404
  • 4
  • 23
  • 39
  • 1
    Itried String s = "<a href=\"http://google.com\">Google</a>"; tv1.setMovementMethod(LinkMovementMethod.getInstance()); tv1.setText(Html.fromHtml(s)); and what I see is "Google" Again no actual anchor link. – Tvd Aug 25 '12 at 10:10
  • @SquireaSquire, I have tested on Emulator only of Api 2.2 & 4.1. – Tvd Aug 27 '12 at 08:17
  • @Tvd i have this code in my own app and it displays a normal link (blue underline, onclick goes to website) – SquiresSquire Aug 27 '12 at 08:42
  • 1
    What is ur system config & which API r u using – Tvd Aug 28 '12 at 06:46
  • @Tvd windows 7 emulator - 2.0, Nexus S 4.1 [Jellybean], Xperia Play 2.1 – SquiresSquire Aug 28 '12 at 07:47
  • I am on Win 7 x64. Tried on emulator 2.2 & 4.1 API 8. The code that I got, works on both emulator versions. – Tvd Aug 28 '12 at 10:36
  • Yrs it works with the code I added as answer. No other way was workable except this one. – Tvd Aug 28 '12 at 17:03
7

I was also facing the same issue I resolved using following

String str_text = "<a href=http://www.google.com >Google</a>";
TextView link;
link = (TextView) findViewById(R.id.link);
link.setMovementMethod(LinkMovementMethod.getInstance());
link.setText(Html.fromHtml(str_text));

for changing the link color to blue use

link.setLinkTextColor(Color.BLUE);
Ganesh Jogam
  • 3,117
  • 1
  • 13
  • 10
7

Here is my simple implementation tested up to Android N.

String termsText = "By registering, you are agree to our";
String termsLink = " <a href=https://www.yourdomain.com/terms-conditions.html >Terms of Service</a>";
String privacyLink = " and our <a href=https://www.yourdomain.com/privacy-policy.html >Privacy Policy</a>";
String allText = termsText + termsLink + privacyLink;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    ((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
    ((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText, Html.FROM_HTML_MODE_LEGACY));
} 
else {
    ((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
    ((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText));
}
Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
user2837615
  • 296
  • 1
  • 5
  • 12
3
txtview.setMovementMethod(LinkMovementMethod.getInstance());

Pass this statement to your textview, and in string.xml set an string as

<string name="txtCredits"> <a href="http://www.google.com"></a></string>

Now pass this string name " android:text="@string/txtCredits" to your xml class where the txtview is there .

bummi
  • 27,123
  • 14
  • 62
  • 101
Naina
  • 49
  • 5
0

use this code autolink-java On GitHub

like this

private String getLink(String string){

    LinkExtractor linkExtractor = LinkExtractor.builder()
            .linkTypes(EnumSet.of(LinkType.URL)) // limit to URLs
            .build();
    Iterable<Span> spans = linkExtractor.extractSpans(string);

    StringBuilder sb = new StringBuilder();
    for (Span span : spans) {
        String text = string.substring(span.getBeginIndex(), span.getEndIndex());
        if (span instanceof LinkSpan) {
            // span is a URL
            sb.append("<a href=\"");
            sb.append(text);
            sb.append("\">");
            sb.append(text);
            sb.append("</a>");
        } else {
            // span is plain text before/after link
            sb.append(text);
        }
    }

    return sb.toString();  // "wow <a href=\"http://test.com\">http://test.com</a> such linked"
}
ibrahim
  • 9
  • 1
  • 3