Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change action bar title color in code

I'm having trouble changing androids action bar title color programmatically for v11 and up. I can get it done in xml but need to change it dynamically in code. How should I go about this? Thanks in advance.

like image 579
jfortunato Avatar asked Mar 29 '12 06:03

jfortunato


People also ask

How can I change action bar title color?

xml in values folder this will change your action bar color.. Replace #666666 with your selected color code for title background color and replace #000000 for your title text color.

How do I change the color of my app bar text?

Go to the app > res > values > themes > themes.

How do I customize my action bar?

This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

The ActionBar title ID is hidden, or in other words, it's internal and accessing it can't be done typically. You can reference it using Resources.getIdentifier then View.findViewById though.

Grab the ID for the action_bar_title

int titleId = getResources().getIdentifier("action_bar_title", "id", "android"); 

Now you can use the ID with a TextView

TextView abTitle = (TextView) findViewById(titleId); abTitle.setTextColor(colorId); 
like image 55
adneal Avatar answered Sep 19 '22 23:09

adneal


You can use a SpannableString and ForegroundColorSpan to set the colour of the title

    Spannable text = new SpannableString(actionBar.getTitle());     text.setSpan(new ForegroundColorSpan(Color.BLUE), 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);     actionBar.setTitle(text); 
like image 43
Willy Avatar answered Sep 20 '22 23:09

Willy