Stack Overflow archive
3 scoreaccepted

Color of title text doesn't change and insert logo on ActionBar

score
3
question views
952
license
CC BY-SA 3.0

Set appcompat icon and title text color using styles:

xml
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="navigationIcon">@drawable/ic_launcher</item>
</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <item name="titleTextStyle">@style/TitleTextStyle</item>
    <item name="displayOptions">showHome|showTitle</item>
</style>

<style name="TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">#A52421</item>
</style>

Set appcompat icon and title text color programmatically:

To set the color on the ActionBar title you need to use SpannableString. The icon is not showing becuase appcompat-v7 sets setDisplayShowHomeEnabled to false by default. Add the following code after you call super.onCreate(Bundle):

java
SpannableString spannableString = new SpannableString(getString(R.string.app_name));
spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, spannableString.toString()
        .length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
getSupportActionBar().setTitle(spannableString);

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.