I'd like to have one of the MenuItem
s in my app's action bar change colour based on its value. There are other items which should retain the default style so I can't apply styles to the whole action bar, it's got to just be to this one item. I've tried using a SpannableString
like so:
MenuItem item = getActionBar().findItem(R.id.item_id);
SpannableString colouredString = new SpannableString("Item");
colouredString.setSpan(new ForegroundColorSpan(
booleanStatement ? Color.RED : Color.GREEN
), 0, colouredString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
item.setTitle(colouredString);
This sets the value correctly, but the item's text colour is unaffected. If I add in
getActionBar().setTitle(colouredString);
then the title is set as expected, with the text colour applied. Is there a way of making SpannableString
work on MenuItem
s as well as the overall action bar title? Or an alternative way of setting the text colour of an action bar menu item?