Android Styling ActionBar Title And SubTitle

By | April 16, 2015

In the previous tutorial we have seen how to custom android actionbar by changing the default background with custom background in this tutorial we will see how to customize the actionbar title and subtitle  with text color , text size , text style .

In Code

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);

In XML

Declare set of color constants inside the res/values/colors.xml

file : res/values/colors.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
     <color name=”Red”>#FF0000</color>
     <color name=”Green”>#008000</color>
     <color name=”Blue”>#0000FF</color>
     <color name=”LightGreen”>#98BD49</color>
     <color name=”Orange”>#FA8400</color>
     <color name=”Pink”>#F50057</color>
     <color name=”Yellow”>#FFF0F0</color>
     <color name=”Black”>#000000</color>
     <color name=”White”>#FFFFFF</color>
</resources>

Adding the below style inside the res/values/style.xml , res/values-v11/style.xml , res/values-v14/style.xml

<resources xmlns:android=”http://schemas.android.com/apk/res/android”>

    <!– Application theme. –>
    <style name=”AppTheme” parent=”@android:style/Theme.Holo.Light.DarkActionBar”>
        <item name=”android:actionBarStyle”>@style/MyActionBar</item>
    </style>

    <!– ActionBar styles –>
    <style name=”MyActionBar” parent=”@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse”>
        <item name=”android:background”>@color/Pink</item>
        <item name=”android:titleTextStyle”>@style/MyActionBarTitleText</item>
        <item name=”android:subtitleTextStyle”>@style/MyActionBarSubTitleText</item>
    </style>

    <!– ActionBar title text –>
    <style name=”MyActionBarTitleText” parent=”@android:style/TextAppearance.Holo.Widget.ActionBar.Title.Inverse”>
        <item name=”android:textColor”>@color/White</item>
        <item name=”android:textStyle”>bold</item>
    </style>

    <!– ActionBar subtitle text –>
    <style name=”MyActionBarSubTitleText” parent=”@android:style/TextAppearance.Holo.Widget.ActionBar.Subtitle”>
        <item name=”android:textColor”>@color/White</item>
        <item name=”android:textStyle”>bold</item>
    </style>

</resources>

Inside manifest file for application tag set the theme property with above defined style name

<application
android:theme=”@style/AppTheme”
———
———