Skip to content

Instantly share code, notes, and snippets.

@Bhavdip
Last active May 18, 2017 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bhavdip/a947c27d5dd02d0b8e3f73e159a19e21 to your computer and use it in GitHub Desktop.
Save Bhavdip/a947c27d5dd02d0b8e3f73e159a19e21 to your computer and use it in GitHub Desktop.
[TextView Clickable] #tags:Android
//how-to-set-the-part-of-the-text-view-is-clickable
SpannableString ss = new SpannableString("Android is a Software stack");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
startActivity(new Intent(MyActivity.this, NextActivity.class));
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
//to-draw-an-underline-below-the-textview-in-android
//1st Approach
//For underling the text in TextView you have to use SpannableString
String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);
//2nd Approach You can make use of setPaintFlags method of TextView to underline the text of TextView.
//For eg.
mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");
You can refer constants of Paint class if you want to strike thru the text.
//3rd Approach
//Make use of Html.fromHtml(htmlString);
String htmlString="<u>This text will be underlined</u>";
mTextView.setText(Html.fromHtml(htmlString));
//OR
txtView.setText(Html.fromHtml("<u>underlined</u> text"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment