Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a portion of a Checkbox's text clickable?

I'm trying to create a link in my textbox's adjacent text. This link however is not a URL, but should act as a button so that I can perform a few tasks in the onItemClick event. I'm basically connecting this to a view that shows our End User License Agreement (hard coded).

How can I accomplish this?

Thanks in advance.

like image 448
PaulG Avatar asked Nov 18 '11 15:11

PaulG


2 Answers

You may want only part of the text to be a clickable link, while the rest of the checkbox behaves as usual, i.e. you can click the other text to toggle the state.

You can set up your checkbox like so:

CheckBox checkBox = (CheckBox) findViewById(R.id.my_check_box);  ClickableSpan clickableSpan = new ClickableSpan() {     @Override     public void onClick(View widget) {         // Prevent CheckBox state from being toggled when link is clicked         widget.cancelPendingInputEvents();         // Do action for link text...     }     @Override     public void updateDrawState(TextPaint ds) {         super.updateDrawState(ds);         // Show links with underlines (optional)         ds.setUnderlineText(true);     } };  SpannableString linkText = new SpannableString("Link text"); linkText.setSpan(clickableSpan, 0, linkText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); CharSequence cs = TextUtils.expandTemplate(     "CheckBox text with link: ^1 , and after link", linkText);  checkBox.setText(cs); // Finally, make links clickable checkBox.setMovementMethod(LinkMovementMethod.getInstance()); 
like image 57
Daniel Schuler Avatar answered Sep 18 '22 12:09

Daniel Schuler


The following code worked for me on KitKat. I am yet to test on below versions of Android.

String checkBoxText = "I agree to all the <a href='http://www.redbus.in/mob/mTerms.aspx' > Terms and Conditions</a>";  checkBoxView.setText(Html.fromHtml(checkBoxText)); checkBoxView.setMovementMethod(LinkMovementMethod.getInstance()); 
like image 39
Gopinath Avatar answered Sep 22 '22 12:09

Gopinath