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, underlined and click-able.
I know hyperlinks in android can be placed with "Linkify"..
and i have referred android docs page
Consider the same above paragraph with "android docs" i want to display in android.....
@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());
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.
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:
<string name="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:
<string name="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!
Use Spannable String
private void setTermsAndTextToClickable(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)){
SpannableString ssString = new SpannableString(termsAndCon);
int startLoc = termsAndCon.indexOf(wordTohyperLink);
int endLoc = termsAndCon.indexOf(wordTohyperLink) + wordTohyperLink.length();
ssString.setSpan(new URLSpan(termsAndConUrl),startLoc,endLoc, 0);
tandcTextView.setText(ssString);
tandcTextView.setMovementMethod(LinkMovementMethod.getInstance());
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(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);
}
}