How to add links to CheckBox text in android?

I am making a registration form where I need a checkbox to agree on a term and condition. So the problem is that the field text contains a link to the open web page (see image below). I am wondering if I can make a checkbox like TextView

using ClickableSpan

.

enter image description here

I have done it so far using below code:

The following method takes a string and returns SpannableString

which is checked.

private SpannableString makeSpannable(String text)
{
String temp = text;
Pattern pattern = Pattern
    .compile("<a href=[\\\"']([\\.^>^<\\w:/]*)[\\\"']>([^>^<.]*)</a>");
Matcher matcher = pattern.matcher(text);
while (matcher.find())
{
    temp = temp.replace(matcher.group(0), (matcher.group(2)));
}

SpannableString linkedString = new SpannableString(temp);
matcher = pattern.matcher(text);
while (matcher.find())
{

    final String href = matcher.group(1);

    int index = temp.indexOf(matcher.group(2));
    ClickableSpan clickable = new ClickableSpan() {

    @Override
    public void onClick(View widget)
    {
        loadLink(href);
    }
    };
    linkedString.setSpan(clickable, index, index
        + matcher.group(2).length(), 0);
}
return linkedString;
}

      

t.setText (makeSpannable ( "Ich akzeptiere die <a href='http://www.google.com'>AGB's</a> und <a href='http://www.google.com'>Datenschutzerklärung</a> des Verlags"

));

Can anyone help me?

+3


source to share





All Articles