0

My code :

<CheckBox
         android:id="@+id/checkBox1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:layout_centerHorizontal="true"
         android:layout_marginBottom="32dp"
         android:text="I agree for terms and conditions"
         android:textColor="#FFFFFF" />

Question

  1. Here for the text "I agree for terms and conditions", I want to add hyperlink for the text "terms and conditions"
  2. How to achieve this
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • possible duplicate of [How do I make a portion of a Checkbox's text clickable?](http://stackoverflow.com/questions/8184597/how-do-i-make-a-portion-of-a-checkboxs-text-clickable) – Leigh Jan 18 '15 at 14:42

4 Answers4

0

You can do like

Pattern p = Pattern.compile("click here");
Matcher m = p.matcher("for more info, click here");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
while(result) {
    m.appendReplacement(sb, "some text which contains pattern to linkfy");
    result = m.find();
}
m.appendTail(sb);
String strWithLink = sb.toString();

textView.setText(Html.fromHtml(strWithLink));
textView.setMovementMethod(LinkMovementMethod.getInstance())
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
0

I accomplished the same thing using a Spannable object and registering a different ClickableSpan object for each one. With the Spannable you have the possibility to style the aspect of the string you want to show (text color, foreground/background color etc)

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Well I disagree with this solution as you may end up in having to support multiple languages for your application and that will force you write horrible size `if-else if-else` code. – Toochka Nov 30 '15 at 21:38
  • No you wouldn't. I am doing my it, supporting six different languages and not using any *horrible size if-else-if* @Toochka – Blackbelt Nov 30 '15 at 21:47
0

In your activity/fragment you add the following:

checkBox.setText(
    Html.fromHtml(
        "<a href=\"http://www.google.com\">I agree for terms and conditions</a> "));
Skywalker
  • 1,717
  • 1
  • 22
  • 25
0

Add this in res->values->string.xml file

<string name="string_name"><a href=/"http://www.example.com">abc </a> and <a href=/"http://www.example2.com">xyz</a></string>

In Code

TextView txt;

txt.setMovementMethod(LinkMovementMethod.getInstance());

in xml file :

android:text="@string/string_name"

User
  • 91
  • 1
  • 5