Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Clickable links in TextView on Android

I'm trying to add multiple links in a textview similar to what Google & Flipboard has done below with their Terms and conditions AND Privacy Policy shown in the screen shot below:

So far I've stumbled on using this approach

textView.setText(Html.fromHtml(myHtml); textView.setMovementMethod(LinkMovementMethod.getInstance());

where myHtml is a href.

But it doesn't give me control I need e.g to launch a fragment etc.

Any idea how they achieve this in the two examples below?

Flipboard terms and conditions view

Google terms and conditions view

like image 690
AndroidEnthusiast Avatar asked Feb 25 '15 13:02

AndroidEnthusiast


People also ask

Can you make a TextView clickable in Android?

In Android, the most common way to show a text is by TextView element. The whole text in the TextView is easy to make clickable implementing the onClick attribute or by setting an onClickListener to the TextView.

What is mean by Linkify in Android?

android.text.util.Linkify. Linkify take a piece of text and a regular expression and turns all of the regex matches in the text into clickable links. This is particularly useful for matching things like email addresses, web URLs, etc. and making them actionable.

Can you have multiple styles inside TextView?

Use multiple widgets if you need precise placement of discrete bits of text. Use inline markup if you, um, need markup inline in a widget. Remember: there is no FlowLayout in Android, so stringing together multiple TextViews to create a paragraph is not truly practical AFAIK.


2 Answers

I think that I'm a little late to share this, but I have achieved the same using SpannableStringBuilder.

Simply initialize the TextView that you want to add 2 or more listeners and then pass that to the following method that I have created:

private void customTextView(TextView view) {         SpannableStringBuilder spanTxt = new SpannableStringBuilder(                 "I agree to the ");         spanTxt.append("Term of services");         spanTxt.setSpan(new ClickableSpan() {             @Override             public void onClick(View widget) {                 Toast.makeText(getApplicationContext(), "Terms of services Clicked",                         Toast.LENGTH_SHORT).show();             }         }, spanTxt.length() - "Term of services".length(), spanTxt.length(), 0);         spanTxt.append(" and");         spanTxt.setSpan(new ForegroundColorSpan(Color.BLACK), 32, spanTxt.length(), 0);         spanTxt.append(" Privacy Policy");         spanTxt.setSpan(new ClickableSpan() {             @Override             public void onClick(View widget) {                 Toast.makeText(getApplicationContext(), "Privacy Policy Clicked",                         Toast.LENGTH_SHORT).show();             }         }, spanTxt.length() - " Privacy Policy".length(), spanTxt.length(), 0);         view.setMovementMethod(LinkMovementMethod.getInstance());         view.setText(spanTxt, BufferType.SPANNABLE);     }  

And in your XML, use android:textColorLink to add custom link color of your choice. Like this:

   <TextView      android:id="@+id/textView1"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="TextView"      android:textColorLink="#C36241" />  //#C36241 - Rust 

And this looks like this:

enter image description here

Hope it helps someone. :)

like image 164
Tushar Gogna Avatar answered Sep 23 '22 08:09

Tushar Gogna


You can use Linkify (android.text.Spannable,java.util.regex.Pattern,java.lang.String)

String termsAndConditions = getResources().getString(R.string.terms_and_conditions); String privacyPolicy = getResources().getString(R.string.privacy_policy);  legalDescription.setText(     String.format(         getResources().getString(R.string.message),         termsAndConditions,         privacyPolicy) ); legalDescription.setMovementMethod(LinkMovementMethod.getInstance());  Pattern termsAndConditionsMatcher = Pattern.compile(termsAndConditions); Linkify.addLinks(legalDescription, termsAndConditionsMatcher, "terms:");  Pattern privacyPolicyMatcher = Pattern.compile(privacyPolicy); Linkify.addLinks(legalDescription, privacyPolicyMatcher, "privacy:"); 

and then you can use the scheme to start an activity for example by adding the scheme in the AndroidManifest:

<intent-filter>     <category android:name="android.intent.category.DEFAULT" />     <action android:name="android.intent.action.VIEW" />     <data android:scheme="terms" />     <data android:scheme="privacy" /> </intent-filter> 

If you want to do a custom action, you can set the intent-filter to your current activity, which will have a singleTop launchmode.

This will cause onNewIntent to be fired where can make your custom actions:

@Override protected void onNewIntent(final Intent intent) {  ...   if (intent.getScheme().equals(..)) {     ..   } } 
like image 34
harrane Avatar answered Sep 25 '22 08:09

harrane