0

I'm trying to set a single character in my TextView to transparent but also underlining that character. The underline should be visible.

Since I'm already using a SpannableStringBuilder I decided to try setting the foreground color for the character to transparent and then adding an underline span:

val foregroundColorSpan = ForegroundColorSpan(Color.TRANSPARENT)
val underlineSpan = UnderlineSpan()
spannableStringBuilder.setSpan(foregroundColorSpan, 2, 3, 0)
spannableStringBuilder.setSpan(underlineSpan, 2, 3, 0)

The only problem is the ForegroundColorSpan also changes the underline color to transparent. How can I prevent the underline from being transparent as well, or how could I hide the single character but also leave it visibly underlined?

I've seen some other solutions for changing the underline color but they used reflection and that is not an option I would like to go with.

Programmer001
  • 2,240
  • 2
  • 19
  • 35
  • Can you replace the character with a space? – Gabe Sechan Oct 12 '18 at 00:04
  • Unfortunately, using a space causes spacing issues with foreign character sets with what I'm trying to do. Keeping the character intact (hence the transparency) with these spannables is a solution I'm working out to avoid these issues. – Programmer001 Oct 12 '18 at 01:17
  • Have you tried a space with an explicit LTR or RTL character? (I'm assuming that's your issue). – Gabe Sechan Oct 12 '18 at 02:05
  • Mainly you'll notice certain languages use glyphs that are 2-byte and others single byte. Then other languages seem to be using a combination. This quick search provides a good visual: https://msdn.microsoft.com/en-us/library/cc194788.aspx What I haven't tried is combining the space (or LTR/RTL character) with the underline span, I'll give it a try tomorrow AM and update if that worked. My worry though is the space could be a different character width than the character being "replaced" (despite using mono space fonts). – Programmer001 Oct 12 '18 at 03:42
  • No luck. For the time being I'm giving in to the reflection with a fallback. If anyone is interested, the capability is easily there but hidden outside of the internal android source. Here are a couple feature requests for this functionality be either opened or be made more accessible: https://issuetracker.google.com/issues/117640279 and https://issuetracker.google.com/issues/37065088 – Programmer001 Oct 13 '18 at 02:40

0 Answers0