Ackman Ackman - 3 months ago 7x
Android Question

How to make a string clickable in Android

<?xml version="1.0" encoding="utf-8"?>
<string name="legalText">

By using this xxxxxxxxxxx app, I agree to the Terms of Use and Notice of Privacy Policy [[need official name]] in this app. I understand that use of this xxxxxxxxx app is at my own risk and discretion. I understand and agree that the health information I give to xxxxxxx in this app is truthful and will be the only source of health information used by the xxxxxxxxxx providers during the course of my evaluation and treatment through xxxxxxxxxx. Also, as part of my use of the xxxxxxxx app, I agree that I will not use this app for any purpose that is prohibited by the xxxxxxxx Terms of Use [[link to Terms]] and Consent to Telehealth [[Link to Consent to Telehealth]]”, and Consent to Request Medical Services [insert link to Consent to Request Medical Services].


I need to put links to other xml pages and html pages at bracket locations in this xml file. How do I do that?


Here's what you can do:

  1. Break that one string into several string resources (atleast separate the app-name and links to a different string resource.
  2. Contruct the full legalText by joining the substrings found in step 1. Use the <a href= "link"><a/> tag for the substrings with link. You can style the rest of the substrings accordingly, as you get the idea.
  3. Use the Html.fromHtml method to show the formatting:

    Spannable text = (Spannable)Html.fromHtml(legalText);
    //this one here to get the links clickable:        
  4. You might need to get rid of extra underlines at this point, therefore use this method:

    private void stripUnderlines(TextView textView) {
        Spannable s = (Spannable)textView.getText();
        URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        for (URLSpan span: spans) {
            int start = s.getSpanStart(span);
            int end = s.getSpanEnd(span);
            span = new URLSpanNoUnderline(span.getURL());
            s.setSpan(span, start, end, 0);