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
How can I fix my problem?
↳ 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.
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)
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.
This is a more general answer to show just a basic working example of a URLSpan
.
// 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With