android -> set different colors for each font of the text

 

TextView text1 = (TextView)findViewById(R.id.text1);
//
SpannableStringBuilder builder = new SpannableStringBuilder(text1.getText().toString());
//ForegroundColorSpan is the text foreground color, BackgroundColorSpan is the text background color
ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.GRAY);
ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE);
ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN);
//
builder.setSpan(redSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(whiteSpan, 1, 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
builder.setSpan(blueSpan, 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(greenSpan, 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//
text1.setText(builder)

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326520767&siteId=291194637