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?
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:And in your XML, use
android:textColorLink
to add custom link color of your choice. Like this:And this looks like this:
Hope it helps someone. :)
With Textoo, this can be achieved like:
res/values/strings.xml:
res/layout/myActivity.xml:
java/myPackage/MyActivity.java:
This approach have the advantages that:
LinksHandler
and no need to define additional intent filterAn easy fix for this would be to use multiple labels. One for each link and one for the text.
Or is it necessary that you only use one label?
This is working for me :
in xml :
In strings.xml
in the activity :
You can use Linkify (android.text.Spannable,java.util.regex.Pattern,java.lang.String)
and then you can use the scheme to start an activity for example by adding the scheme in the AndroidManifest:
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:
Here's my solution:
First we need to have clickable links in our TextView:
Here's my TextView in the xml layout, do not add any links handling parameters.
In strings file, I added the resource text with html tags
In onCreateView set LinkMovementMethod to the TextView
Now the TextView links are clickable.
Second, We need to Handle these clicks:
In my Manifest file, I added intent-filter for "terms" and "privacy" and Single Instance Launch Mode
In MyActivity, override onNewIntent to catch privacy and terms intents