Android: TextView with concise colors and fonts

Ok, so I understand that this should be a pretty straightforward process.

I read the following questions:

The advice seems pretty similar to all of these questions and answers. I am trying to avoid HTML methods and use SpannableString

and instead SpannableStringBuilder

. Ultimately, I would like to be able to use several different fonts in the same TextView, but for now I would just like to figure out how to work with multiple colors.

I am trying to implement these methods this way:

// Get a typeface for my custom font
String regularFontPath          = "fonts/Abel-Regular.ttf";
Typeface regularTf              = Typeface.createFromAsset(getActivity().getAssets(), regularFontPath);

// Set the label typeface (this part is working)

// Create a spannable builder to build up my
// TextView content from data
SpannableStringBuilder builder  = new SpannableStringBuilder();

// These colors are defined and working well in other parts of my app
ForegroundColorSpan ltGraySpan  = new ForegroundColorSpan(R.color.light_gray);
ForegroundColorSpan dkGraySpan  = new ForegroundColorSpan(R.color.dark_gray);

// mCodesList has good data and the actual data output from this
// loop is correct. Just the styling is wrong
for (int i = 0; i < mCodesList.size(); i = i + 1) {
    ParseObject code            = mCodesList.get(i);
    String value                = code.getString("value") + " | ";
    if (i > 0) {
        // I want new codes to be on a new line (this works)
        value                   = "\n" + value;
    SpannableString valueSpan   = new SpannableString(value);
    valueSpan.setSpan(ltGraySpan, 0, value.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    String loc                  = code.getString("location");
    SpannableString locSpan     = new SpannableString(loc);
    locSpan.setSpan(dkGraySpan, 0, loc.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


The end result is one that TextView

contains the correct text content. TextView

- correct font. But all content TextView

is my color @color/light_gray

. I'm not sure why, because in the XML layout file I had my color specified @color/dark_gray

(which I expect to override it by setting the text with Spannable

). Even if I changed both objects ForegroundColorSpan

to use R.color.dark_gray

it TextView

will still be light gray. I don't see anywhere in my code where I am setting the text color, so I am really at a loss.

I am running this on LG Optimus G Pro which is running 4.4.2. I have another one TextView

where I need to get multiple colors and fonts and even highlight some parts of the text, so this is a pretty big deal for me. Where am I going wrong?


source to share

2 answers

use getResource().getColor(R.color.light_gray)

to get the color you transfer to ForegroundColorSpan

. I doubt he is extracting it from the inside for you. You probably need to create a new one ForegroundColorSpan

on every iteration. Can't reuse it



You can use SpannableStringBuilder because it implements from spannable and CharSequence also you can do anything with the following

TextView txtTest = (TextView) findViewById(;
String text = "This is an example";    
final SpannableStringBuilder str = new SpannableStringBuilder(text);
str.setSpan(new TypefaceSpan("monospace"), 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new TypefaceSpan("serif"), 9, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.white)), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.grey)), 6, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


I am adding colors.xml to values

<color name="black">#000000</color>
<color name="grey">#DCDCDC</color>
<color name="white">#FFFFFF</color>




All Articles