I have the following code to set a color span on multiple subStrings of the same string.
fun getColoredText(text: String, @ColorInt color: Int, vararg coloredSubText: String): Spannable {
val spannable = SpannableString(text)
for (textToColor in coloredSubText) {
val start = text.indexOf(textToColor)
spannable.setSpan(
ForegroundColorSpan(color),
start,
start + textToColor.length - 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannable.
}
return spannable
}
I provide the following arguments:
getColoredText("This is my full length of the string", someColorValue, "my", "length")
But everything after "my full length of the string"
gets colored.
Could someone please help figure out what is wrong with the above method?
thanks
The above code is fine. The issue was that i was trying to set clickable span on a spannable with color. And the clickable span overrides the other colors. The way to set it as follows: