Last active
February 3, 2019 16:46
-
-
Save MarkNjunge/67d0711a6e9c2da3e2cf493313374947 to your computer and use it in GitHub Desktop.
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
private fun updateTextView(plainText: String, ranges: MutableList<Pair<Int, Int>>) { | |
val spannableString = SpannableString(plainText) | |
ranges.forEach { range -> | |
// Make the range | |
spannableString.setSpan(object : ClickableSpan() { | |
override fun onClick(widget: View) { | |
// Remove clicked range from the list | |
ranges.remove(Pair(range.first, range.second)) | |
updateTextView(plainText, ranges) | |
} | |
}, range.first, range.second, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) | |
// Set the background to black to give it the hidden effect | |
spannableString.setSpan( | |
BackgroundColorSpan(Color.BLACK), | |
range.first, | |
range.second, | |
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | |
) | |
// The foreground color (text color) should match the background | |
spannableString.setSpan( | |
ForegroundColorSpan(Color.BLACK), | |
range.first, | |
range.second, | |
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | |
) | |
} | |
textView.text = spannableString | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment