0

I'm working on a small project and I can not get the expected result. I would like change color odd words of a string and the other another color. Can anyone help? I leave here my current code:

   public void updateMessages() {
        String groupMessage = "Admin says: Hi people\n Admin says: What's up?";

        TextView groupMessageBox = (TextView) this
                .findViewById(R.id.groupMessageBox);

        String[] str_array = groupMessage.split("\n|\\:");

        //Result str_array == Admin says, Admin says

        for (int i = 0; i < str_array.length; i++) {
//Get values of array           
String val1 = str_array[i];


//Only numbers pairs
if ( i % 2 == 0 ) { 
                //How to change the result to blue, for example?
                  Log.e("",val1);
            }else{
                  Log.e("",val1);
                //How to change the result to red, for example?
            }
        }
        groupMessageBox.setText(groupMessage);
    }

Greetings!

Adrian
  • 15
  • 4
  • 1
    Have a look [here](http://stackoverflow.com/questions/8405661/is-it-possible-to-change-the-text-color-in-a-string-to-multiple-colors-in-java) – Steve Benett Aug 30 '14 at 15:22

1 Answers1

0

If you want to change the color of val1 to red, for example:

String coloredString = "<font color="#FF0000">" + val1 + "</font>";
str_array[i] = coloredString;

Then outside the for loop, you'd probably want to rejoin the text and reset the textView:

String coloredMessage = // Join the strings together
groupMessageBox.setText(Html.fromHtml(coloredMessage));
AAA
  • 1,364
  • 1
  • 10
  • 15