Reputation: 12057
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.
Upvotes: 46
Views: 50692
Reputation: 99
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 😊
Upvotes: 0
Reputation: 1012
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
Upvotes: 1
Reputation: 55340
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);
Upvotes: 16
Reputation: 950
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);
Upvotes: 85
Reputation: 30794
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);
Upvotes: 82
Reputation: 1104
Another way is using Html
getSupportActionBar().setTitle((Html.fromHtml("<font color=\"#FF4444\">" + getString(R.string.some_string) + "</font>")));
Upvotes: 35
Reputation: 2062
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;
Upvotes: 9