44

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.

jfortunato
  • 11,607
  • 3
  • 19
  • 14

7 Answers7

83

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);
Willy
  • 930
  • 6
  • 4
  • 12
    This should be the accepted solution, relying on an internal API (action_bar_title id) is a bad idea as it might change without notice. – fernandohur Sep 04 '14 at 23:28
  • 2
    This is the best answer. Specifically on KitKat using v7 appcompat library FAILs on action_bar_title id solution. Use this answer! – Codeversed Nov 26 '14 at 04:12
  • this is awesome answer, thanks about that, BTW with the same approach you can make Underline on the toolbar title text. But this is for another discussion . – Stoycho Andreev Jul 22 '16 at 16:54
  • Works For SupportActionBar Too – RAINA Nov 18 '19 at 09:13
82

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);
adneal
  • 30,484
  • 10
  • 122
  • 151
  • 1
    This thing doesn't work for me.To make sure, there is no static title in actionbar, so I am using "action_bar_title" within the getIdentifier method call. Same goes for "id" and "android". I am using v7-appcompat library. – MSIslam Jan 02 '14 at 17:36
  • 4
    If this is not working for you, make sure you use `setTextColor(getResources().getColor(R.color.myColor))` instead of `setTextColor(R.color.myColor)`. Works smoothly for me on Android 4.0+. – Jonik Jan 14 '14 at 10:48
  • Shortest & Easiest implementation. Thanks Dude :) – arshu Mar 04 '14 at 11:35
  • Any chance you could add how-to adress the subtitle aswell? – Mathijs Segers Mar 27 '14 at 08:55
  • @MathijsSegers Mhmm, just use `action_bar_subtitle` instead. – adneal Mar 27 '14 at 08:56
  • @adneal I thought so, maybe add it to the answer. I couldn't find that part googling. Thanks a lot (can't do this by styles since It's customizable by API). – Mathijs Segers Mar 27 '14 at 08:59
  • If "doesn't work" means you get abTitle == null, then check you don't have something like actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); during action bar initialisation – esteewhy Jun 20 '14 at 13:05
  • 1
    Please note that the `setTitle()` method on ActionBar accepts a Spannable as well, as @wily has mentioned in his answer. Much better when some parts of the text needs to be a different colour. – Diederik Oct 21 '14 at 07:20
  • It does n't work for me..In AppcompatActivity.Can u please suggest anyother way to do this.. – Raja Jawahar Nov 12 '15 at 13:01
  • relying on an internal API (action_bar_title id) is a bad idea as it might change without notice – Tigran Babajanyan Jan 23 '19 at 12:53
  • Answer needs to be updated, update the answer because it looks like this doesn't work anymore – TechGeek May 30 '22 at 06:58
35

Another way is using Html

getSupportActionBar().setTitle((Html.fromHtml("<font color=\"#FF4444\">" + getString(R.string.some_string) + "</font>")));
IvanP
  • 1,064
  • 13
  • 16
16

If using the v7 appcompat library (tested with r22) then you can call setTitleTextColor() on the Toolbar object that substitutes the action bar for all API levels. For example:

Toolbar actionBarToolbar = (Toolbar)activity.findViewById(R.id.action_bar);
if (actionBarToolbar != null)
    actionBarToolbar.setTitleTextColor(Color.RED);
matiash
  • 54,791
  • 16
  • 125
  • 154
  • When up upgraded my project to compile for Lollipop, this answer is the only correct one (Adneal's answer crashes) – Fraser Dec 21 '14 at 21:23
  • 1
    I am using support library 21.0.3 and this is not working anymore, if I have not done any error obviously. At first try the IvanP's solution is working. – Čikić Nenad Feb 18 '15 at 03:43
  • @ČikićNenad Fixed for appcompat r22 (at least, should work with r21 too). – matiash Mar 30 '15 at 16:15
9

If you use Sherlock Actionbar you may use the sherlock-actionbar-id for supported actionbars (Android below 3.0)

int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");    
if ( 0 == titleId ) 
        titleId = com.actionbarsherlock.R.id.abs__action_bar_title;
salcosand
  • 2,022
  • 4
  • 24
  • 27
  • 1
    Cant just do this: getSherlockActivity().getSupportActionBar().setTitle("TITLE"); ?? – marchinram Apr 04 '13 at 05:27
  • 2
    Of cause you can do so, when you just want to change the title. but this question was about to change the title-color. – salcosand Apr 12 '13 at 11:08
  • Therefore you need to get the TextView in the ActionBar and to get there you need the id for the TextView. And the id is different in Sherlock ActionBar and the nativ one. – salcosand Apr 12 '13 at 11:15
1

Using Kotlin and SupportActionBar Use this ->

val mSpannableText = SpannableString(supportActionBar?.title)
    mSpannableText.setSpan(
        ForegroundColorSpan(Color.BLUE),
        0,
        mSpannableText.length,
        Spannable.SPAN_INCLUSIVE_INCLUSIVE
    )
    supportActionBar?.title = mSpannableText
RAINA
  • 802
  • 11
  • 22
0

For custom color to work, you need to use a NoActionBar eg android:Theme.NoTitleBar or one of its decendants, then manually add the Toobar in your layout, then setSupportActionBar in your activity's onCreate(),

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Then you can set the title color in toolbar xml app:titleTextColor="@android:color/white"

See below;

<androidx.appcompat.widget.Toolbar
    
  app:navigationContentDescription="@string/abc_action_bar_up_description"
  app:titleTextColor="@android:color/white"
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="?attr/actionBarSize"
  app:popupTheme="@style/AppTheme.PopupOverlay"
  app:navigationIcon="?attr/homeAsUpIndicator"
  app:title="@string/app_name" 
  android:minHeight="@dimen/abc_action_bar_default_height_material" />

Atleast this work form me