Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URLSpan in SpannableString

Tags:

android

I am following this example in using SpannableString: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/text/Link.html

I am trying to create a string which has 'R.string.text1' following by R.string.text2 but R.string.text2 (has 10 characters) in URL format:

SpannableString ss = new SpannableString(getString(R.string.text1));
ss.setSpan(new URLSpan(getString(R.string.text2)), 0, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

But what I am getting is

  1. I don't see the string R.string.text2 at all
  2. and only the first 10 characters is in URL format

How can I fix my problem?

like image 450
michael Avatar asked Oct 07 '11 04:10

michael


People also ask

What is URLSpan?

↳ android.text.style.URLSpan. Implementation of the ClickableSpan that allows setting a url string. When selecting and clicking on the text to which the span is attached, the URLSpan will try to open the url, by launching an an Activity with an Intent#ACTION_VIEW intent.

What is SpannableStringBuilder?

SpannableStringBuilder. append(CharSequence text, Object what, int flags) Appends the character sequence text and spans what over the appended part. SpannableStringBuilder. append(CharSequence text, int start, int end)

How do I use Linkify?

To call linkify in android, we have to call linkify. addLinks(), in that method we have to pass textview and LinkifyMask. Linkify. WEB_URLS: It going make URL as web url, when user click on it, it going to send url to default web browsers.


1 Answers

This is a more general answer to show just a basic working example of a URLSpan.

enter image description here

// set up spanned string with url
SpannableString spannableString = new SpannableString("Click here for more.");
String url = "https://developer.android.com";
spannableString.setSpan(new URLSpan(url), 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

// set to textview
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance()); // enable clicking on url span 
like image 157
Suragch Avatar answered Oct 20 '22 20:10

Suragch