Skip to content Skip to sidebar Skip to footer

Hyperlink In Android

I have a question: How can I place a hyperlink WITHIN a text-block of normal text? I have some text and in the text I mention a URL and I want this URL to be in a different color,

Solution 1:

@Fedor.... i have found to give links from API Demos .

this approach is finally be used for giving link in between text of paragraph.

     SpannableString ss = new SpannableString("text4: Click here 
                                                  to dial the phone.");

    ss.setSpan(new StyleSpan(Typeface.ITALIC), 0, 6,
           Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,
           Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextView t4 = (TextView) findViewById(R.id.TextView01);
    t4.setText(ss);
    t4.setMovementMethod(LinkMovementMethod.getInstance());

Solution 2:

Take a look at that TextView underline phone number and hyperlink

But I can see Linkify doesn't provide you the functionality you need. Not a problem, you can implement it manually. Just add ClickableSpans and handlre click. Code sample here http://www.orangeapple.org/?p=354.

Solution 3:

I know this is an old question, but this is a problem I recently struggled with, and I found no StackOverflow answer that helped me. So after extensive experimentation, I found a solution and thought I would share it for posterity.

In your strings.xml, use HTML tags and the markers:

<stringname="example"><![CDATA[I know hyperlinks in android can be placed with "Linkify".. and i have referred <a href="http://developer.android.com/reference/android/text/util/Linkify.html">android docs</a> page]]></string>

In your XML, make sure to NOT add any special attributes. Do NOT use android:autolink

In your Java code, you must use fromHtml, Linkify, and LinkMovementMethod. Make sure to call Linkify before LinkMovementMethod

exampleTextView.setText(Html.fromHtml(getString(R.string.example)));
Linkify.addLinks(exampleTextView, Linkify.WEB_URLS);
exampleTextView.setMovementMethod(LinkMovementMethod.getInstance());

Another caveat: if you are using these HTML links, where a link has display text that does not match the URL (Google and www.google.com), do NOT put raw URLs in the string. So:

<stringname="example"><![CDATA[www.google.com I know hyperlinks in android can be placed with "Linkify".. and i have referred <a href="http://developer.android.com/reference/android/text/util/Linkify.html">android docs</a> page]]></string>

Will turn "www.google.com" into a link, but will not turn "android docs" into a link.

This was very frustrating for me to figure out, as I tried many different combinations and followed many online examples, none of which worked until I found just the right magic combination described above. Hope it helps someone else!

Solution 4:

Use Spannable String

privatevoidsetTermsAndTextToClickable(TextView tandcTextView, String termsAndCon, String wordTohyperLink, String termsAndConUrl){
    if (TextUtils.isEmpty(termsAndCon)){
        tandcTextView.setVisibility(View.GONE);
        return;
    }

    if (!TextUtils.isEmpty(termsAndCon) && !TextUtils.isEmpty(wordTohyperLink) && !TextUtils.isEmpty(termsAndConUrl)){
        SpannableStringssString=newSpannableString(termsAndCon);
        intstartLoc= termsAndCon.indexOf(wordTohyperLink);
        intendLoc= termsAndCon.indexOf(wordTohyperLink) + wordTohyperLink.length();
        ssString.setSpan(newURLSpan(termsAndConUrl),startLoc,endLoc, 0);
        tandcTextView.setText(ssString);
        tandcTextView.setMovementMethod(LinkMovementMethod.getInstance());

        ClickableSpanclickableSpan=newClickableSpan() {
            @OverridepublicvoidonClick(View widget) {
                // We display a Toast. You could do anything you want here.
                Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();

            }
        };
        ssString.setSpan(clickableSpan, startLoc, endLoc, 0);
    }else{
        tandcTextView.setText(termsAndCon);
    }

}

Post a Comment for "Hyperlink In Android"