Last active
August 29, 2015 14:02
-
-
Save laaptu/b05330b0c296a3b6a91c to your computer and use it in GitHub Desktop.
How to make a Texview clickable, underline and change color to certain portion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Terms of Service | |
| String tobeClickable = getString(R.string.tos); | |
| int index = getString(R.string.terms_of_service).indexOf(tobeClickable); | |
| int length = index + tobeClickable.length(); | |
| //I agree to FireSonar's Terms of Service | |
| Spannable spannableString = new SpannableString( | |
| getString(R.string.terms_of_service)); | |
| ClickableSpan clickableSpan = new ClickableSpan() { | |
| @Override | |
| public void onClick(View widget) { | |
| System.out.println("I am clicked"); | |
| } | |
| }; | |
| spannableString.setSpan(clickableSpan, index, length, | |
| Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); | |
| spannableString.setSpan(new ForegroundColorSpan(getResources() | |
| .getColor(R.color.fs_color)), index, length, | |
| Spannable.SPAN_INCLUSIVE_INCLUSIVE); | |
| spannableString.setSpan(new UnderlineSpan(), index, length, | |
| Spannable.SPAN_INCLUSIVE_INCLUSIVE); | |
| termsOfService.setText(spannableString); | |
| //this is required for click to work | |
| termsOfService.setMovementMethod(LinkMovementMethod.getInstance()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment