Skip to content

Instantly share code, notes, and snippets.

@booknara
Last active August 29, 2015 14:11
Show Gist options
  • Save booknara/a7d29317d6937c84d531 to your computer and use it in GitHub Desktop.
Save booknara/a7d29317d6937c84d531 to your computer and use it in GitHub Desktop.
How to set the part of the text view is clickable like "Terms of Service" or "Privacy Policy"
private void setClickableMessage(TextView textView) {
String message = "By signing up, you agree to our Terms of Service";
String linkText = "Terms of Service";
int indexStart = message.indexOf(linkText);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(message, TextView.BufferType.SPANNABLE);
Spannable spannable = (Spannable)textView.getText();
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
/* do something */
startActivity(new Intent(MyActivity.this, NextActivity.class));
}
@Override
public void updateDrawState(TextPaint ds){
ds.setUnderlineText(true);
ds.setColor(Color.rgb(255, 255, 255));
}
};
try {
spannable.setSpan(clickableSpan, indexStart, indexStart + linkText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (Exception e) {
Log.e("TAG", "Exception");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment