I've been trying a whole day to get this to work. I've done this before but lost the source code when my drive crashed. The call:
textView.setText(getSpannableString("1Blue 2Red 5Green", false, MainActivity.this));
Should put the text Blue Red Green coloured in respective colours in the textview. But for some reason only the last 'e' in "Blue" turns blue, last 'd' in "Red" and last 'n' in "Green" have the right colour. Anyone can spot my mistake? The idea is to put a colorspan on each character.
public static SpannableString getSpannableString(String text, Boolean startWithGrey, Context context) {
String cutText=text.replace("0","").replace("1","").replace("2","").replace("5","");
int pos = 0;
ForegroundColorSpan fcs;
int colour;
SpannableString span = new SpannableString(cutText);
if (!startWithGrey){
colour=0;
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_0));
} else {
colour=3;
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_3));
}
for (int i = 0; i < text.length(); i++) {
switch (text.charAt(i)) {
case '0':
if (!startWithGrey){
colour=0;
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_0));
} else {
colour=3;
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_3));
}
break;
case '1':
colour = 1;
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_1));
break;
case '2':
colour = 2;
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_2));
break;
case '5':
colour = 5;
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_5));
break;
default:
span.setSpan(fcs, pos, pos+1 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
pos++;
break;
}
}
return span;
}
And in color.xml:
<color name="text_colour_0">#000000</color>
<color name="text_colour_1">#0000FF</color>
<color name="text_colour_2">#FF0000</color>
<color name="text_colour_3">#A8A8A8</color>
<color name="text_colour_5">#00FF00</color>
Ok guys, thanks alot for the help. I couldn't use your code snippets but they did help me understand what I was doing wrong. I merely moved the colorspan one character at a time, until I created a new colorspan and moved that one a character at a time. That's why only the last character in each was coloured. So I intruduced a new variable: start. The start of each colorspan. I ended up with this. I know you fancy coders could do this in about a fifth of the lines I'm using. I'm not a fancy coder but at least it works now! Thanks again!
public static SpannableString getSpannableString(String text, Boolean startWithGrey, Context context) {
if (text != null) {
String cutText = text.replace("@", "").replace("%", "").replace("0", "").replace("1", "").replace("2", "").replace("3", "").replace("4", "").replace("5", "");
text = text.replace("@", "").replace("%", "");
int pos = 0;
int start = 0;
ForegroundColorSpan fcs;
SpannableString span = new SpannableString(cutText);
if (!startWithGrey) {
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_0));
} else {
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_3));
}
for (int i = 0; i < text.length(); i++) {
switch (text.charAt(i)) {
case '0':
if (!startWithGrey) {
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_0));
} else {
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_3));
}
start = pos;
break;
case '1':
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_1));
start = pos;
break;
case '2':
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_2));
start = pos;
break;
case '3':
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_3));
start = pos;
break;
case '4':
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_4));
start = pos;
break;
case '5':
fcs = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_colour_5));
start = pos;
break;
default:
span.setSpan(fcs, start, pos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
pos++;
break;
}
}
return span;
} else {
return new SpannableString("");
}
}