Skip to content

Instantly share code, notes, and snippets.

@MarkNjunge
Last active February 3, 2019 16:46
Show Gist options
  • Save MarkNjunge/67d0711a6e9c2da3e2cf493313374947 to your computer and use it in GitHub Desktop.
Save MarkNjunge/67d0711a6e9c2da3e2cf493313374947 to your computer and use it in GitHub Desktop.
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