Android hyperlink

I have a question: how to place a WITHIN hyperlink in a text block of plain text? I have text, and in the text I mention a URL, and I want this URL to be a different color, underline and click a button.

I know that hyperlinks in android can be placed using "Linkify". and i called android docs page

Consider the same paragraph above as the "android documents" that I want to display in android .....

+13
android hyperlink anchor
Jul 08 '10 at 13:25
source share
5 answers

@Fedor .... I found a link from the Demos API .

this approach is finally used to provide a link between paragraph text.

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()); 
+14
Jul 09 '10 at 4:19
source share

@PM - I think you are looking for a custom link for Android, I hope this helps you get what you want http://android-geek.blogspot.com/2011/04/linkify-text-in-android .html

+5
Apr 15 2018-11-11T00:
source share

Look at the text number and TextView hyperlink

But I see that Linkify does not provide you the necessary functionality. Not a problem, you can implement it manually. Just add ClickableSpans and click the handle. Sample code here http://www.orangeapple.org/?p=354 .

+1
Jul 08 '10 at 13:39
source share

I know this is an old question, but this is a problem that I recently ran into, and I did not find the StackOverflow answer that helped me. Therefore, after extensive experiments, I found a solution and thought that I would share it with a descendant.

In your strings.xml file, use HTML tags and 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 that no special attributes are added. DO NOT use android: autolink

In your Java code, you should use fromHtml, Linkify, and LinkMovementMethod. Be 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 use these HTML links where the link has display text that does not match the URL (Google and www.google.com), DO NOT put raw URLs in a 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> 

Turn "www.google.com" into the link, but do not include the "android docs" in the link.

It was very difficult for me to figure it out, because I tried many different combinations and followed a lot of online examples, none of which worked until I found only the correct combination of magic described above. Hope this helps someone else!

0
Apr 20 '16 at 20:49
source share

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); } } 
0
Aug 11 '17 at 5:42 on
source share



All Articles