I had a need to have a CheckBox
that contained an integrated clickable area to bring up another screen in the app. This solution works wonderfully:
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());
...except that I am running into the same problem as mentioned in this comment. When the user clicks the ClickableSpan
, while the click gets registered and handled by the span, the CheckBox
also briefly goes into the pressed state. The box of the CheckBox
behaves as though the user clicked it, though it does not toggle the actual checked state.
You can see the effect in this GIF — watch the actual box of the CheckBox
on the left:
I still want that pressed state when the user clicks on the rest of the CheckBox
, so I do not want to try changing a background or something to eliminate the pressed state. Is there a way from the ClickableSpan
to say "please do not transition to the checked state"?