Search code examples
androidkotlintextview

How to find special chain charactes in a string and change color?


My string could look like this: Numbers: [3] or Numbers: [213]

How I can find [] and number inside it and change color of [] and number?


Solution

  • This problem can be divided into 2 sections

    1. the colour change. You have to create a string class in this format
     var text :String = "<font color=#000>[</font><font color=#6ab04c>213</font><font color=#000>]</font>";
    
    
    textview.setText(Html.fromHtml(text));
    
    1. Finding the values.

    In the textwatcher search for the occurrence of the '[' and then format the string accordingly. so a non optimal solution would be str1= "[" str2="219" str3="]"

    Now construct the string as mentioned in the section and then set to the textview