0

Is it possible to set multiple colors for different pieces of text inside a TextView with if condition?

Here is my code:

mColoredText = findViewById(R.id.questionText);
        String mColoredString = "BLACK RED GREEN YELLOW ORANGE BLUE WHITE";
        SpannableStringBuilder builder = new SpannableStringBuilder();

        if(mColoredString.contains("RED")) {
            String red = "RED";
            SpannableString redSpannable = new SpannableString(red);
            redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
            builder.append(redSpannable);
        }

        if(mColoredString.contains("YELLOW")) {
            String yellow = "YELLOW";
            SpannableString whiteSpannable = new SpannableString(yellow);
            whiteSpannable.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, yellow.length(), 0);
            builder.append(whiteSpannable);
        }

        if(mColoredString.contains("BLUE")) {
            String blue = "BLUE";
            SpannableString blueSpannable = new SpannableString(blue);
            blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, blue.length(), 0);
            builder.append(blueSpannable);
        }

        mColoredText.setText(builder, TextView.BufferType.SPANNABLE);

But the end result is always print: RED YELLOW BLUE with it's color, just three text.

I expect BLACK RED GREEN YELLOW ORANGE BLUE WHITE written all together, and white color applicable if no spannable color.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Bonnie7
  • 19
  • 1
  • 10

2 Answers2

0

You create a new builder and add only RED YELLOW and BLUE colors to it. That is why you see always RED YELLOW BLUE.

If you want to update the original text then you have to make a SpannableString from it first.

SpannableString mColoredString = new SpannableString("BLACK RED GREEN YELLOW ORANGE BLUE WHITE");

if (mColoredString.toString().contains("RED")) {
    mColoredString.setSpan(new ForegroundColorSpan(Color.RED), 6, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (mColoredString.toString().contains("YELLOW")) {
    mColoredString.setSpan(new ForegroundColorSpan(Color.YELLOW), 16, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (mColoredString.toString().contains("BLUE")) {
    mColoredString.setSpan(new ForegroundColorSpan(Color.BLUE), 30, 34, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

questionText.setText(mColoredString);

Please read this article for a detailed explanation.

EDIT:

If you don't know the start and end positions then you have to calculate them:

if (mColoredString.toString().contains("YELLOW")) {
    int start = mColoredString.toString().indexOf("YELLOW");
    int end = start + "YELLOW".length();
    mColoredString.setSpan(new ForegroundColorSpan(Color.YELLOW), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Blehi
  • 1,990
  • 1
  • 18
  • 20
  • Thanks, thats works like a charm. But when I generate automatic text within: SpannableString mColoredString = new SpannableString(AUTOMATIC_TEXT); I cannot count start and end of letter. Like 6, 9 for RED. – Bonnie7 Jun 25 '18 at 07:29
  • So if I change the colored STRING, that code error. For example if new SpannableString changed with BROWN RED BLUE GREEN ORANGE GREY. Please help, how to count first and last letter? – Bonnie7 Jun 25 '18 at 07:33
  • You need to calculate the positions manually. Please see the updated answer. – Blehi Jun 25 '18 at 07:41
0

Try this code..

    textView = findViewById(R.id.tvData);
    String mColoredString = "BLACK RED GREEN YELLOW ORANGE BLUE WHITE";
    SpannableStringBuilder builder = new SpannableStringBuilder();

    String strArray[] = mColoredString.split(" ");
    for (int i = 0; i < strArray.length; i++) {
        if (strArray[i].equals("RED")) {
            SpannableString redSpannable = new SpannableString(strArray[i]);
            redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, strArray[i].length(), 0);
            builder.append(redSpannable);
        } else if (strArray[i].equals("YELLOW")) {
            SpannableString whiteSpannable = new SpannableString(strArray[i]);
            whiteSpannable.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, strArray[i].length(), 0);
            builder.append(whiteSpannable);
        } else if (strArray[i].equals("BLUE")) {
            SpannableString blueSpannable = new SpannableString(strArray[i]);
            blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, strArray[i].length(), 0);
            builder.append(blueSpannable);
        } else {
            builder.append(strArray[i]);
        }

    }
    textView.setText(builder);

}