Thursday, February 21, 2019

Color string Android


We can color string in xml file.
android:textColor="#00F"
If want to do in code.
TextView.setTextColor(Color.parseColor("#800080"));
If we want to color a string line with multicolor, we have to use SpannableString.
String text="This is multicolor text line";
SpannableString s = new SpannableString(text);
s.setSpan(new ForegroundColorSpan(Color.RED), 0, 8, 0);
s.setSpan(new ForegroundColorSpan(Color.parseColor("#008000")), 8, 15, 0);
s.setSpan(new ForegroundColorSpan(Color.parseColor("#0000ff")), 16, 27, 0);
tv.setText(s);





Run to see a multi color string android.
Now we have a string contain many lines, we want the lines have different color. Normaly, we set one line to one textView and color string textView. But use SpannableStringBuilder  we can do it in one textView.
For example, we have a string contain seven text lines, from Monday to Sunday, we want to set different colors in one textView.
Declare an arraylist.
ArrayList<String> ar = new ArrayList<String>();
Add elements.
ar.add("Monday");
ar.add("Tuesday");
ar.add("Wednesday");
ar.add("Thursday");
ar.add("Friday");
ar.add("Saturday");
ar.add("Sunday");
Use for command to loop through arraylist.
     SpannableString s1 = new SpannableString("\n");
          SpannableStringBuilder builder = new SpannableStringBuilder();
for (int i = 0; i < ar.size(); i++) {
     if (ar.get(i).equals("Monday")) {
SpannableString redSpannable = new SpannableString(ar.get(i));
                     redSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#FF7F50")), 0, ar
                               .get(i).length(), 0);
               builder.append(redSpannable);
               builder.append(s1);                
} else if (ar.get(i).equals("Tuesday")) {
SpannableString redSpannable = new SpannableString(ar.get(i));
                     redSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#0000ff")),
                               0, ar.get(i).length(), 0);
                     builder.append(redSpannable);
                     builder.append(s1);
} else if (ar.get(i).equals("Wednesday")) {
SpannableString redSpannable = new SpannableString(ar.get(i));
                     redSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#ff008000")),
                               0, ar.get(i).length(), 0);
                     builder.append(redSpannable);
                     builder.append(s1);
} else if (ar.get(i).equals("Thursday")) {SpannableString redSpannable = new SpannableString(ar.get(i));
                     redSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#800000")),
                               0, ar.get(i).length(), 0);
          builder.append(redSpannable);
          builder.append(s1);
     } else if (ar.get(i).equals("Friday")) {
                     SpannableString redSpannable = new SpannableString(ar.get(i));
redSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#00FF00")),
                               0, ar.get(i).length(), 0);
     builder.append(redSpannable);
     builder.append(s1);
     }
     else if (ar.get(i).equals("Saturday")) {
SpannableString redSpannable = new SpannableString(ar.get(i));
     redSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#FF00FF")),
                               0, ar.get(i).length(), 0);
          builder.append(redSpannable);
          builder.append(s1);
     }
else {
SpannableString redSpannable = new SpannableString(ar.get(i));
                     redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, ar
                               .get(i).length(), 0);
     builder.append(redSpannable);
     builder.append(s1);
}
}
     builder.delete(builder.length() - 1, builder.length());
     tv.setText(builder);
Press Ctrl+Shift+O to import libraries. The result will look like this.

No comments:

Post a Comment