Is there a way to hide text in a TextView?

Viewed 36615

Is there a way to hide some (but not all) of the text in a TextView? I tried setting the size to 0 with AbsoluteSizeSpan, but that doesn't have any visual effect that I see. (You can set the size to 1, and you essentially get bumpy lines instead of readable text. Cute, but not quite what I'm after.)

By hide, I mean go away, not be visible and not take up space. Drawing text with the same color as the background isn't what I'm looking for.

I realize I can just replace the text in the TextView with only the text I want to display, but I'm already using spans to do a lot more dynamic styling, and something like a HiddenSpan would be useful. Does it exist?

8 Answers

I've figured out a way. Let's use SpannableString and Color.TRANSPARENT

For example: <> Original text : "Hello dude"

<> Expected text : "_____ dude"

  • Here i will use Kotlin:

val spannableString = SpannableString("Hello dude")

spannableString.setSpan(ForegroundColorSpan(Color.TRANSPARENT), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

yourTextView.text = spannableString

Related