Hector
Hector

Reputation: 5356

Change Android Actionbar subtitle colour

I wish to customize the actionBar of my android application.
I am using Actionbar Sherlock.
My min target sdk is 14, target sdk 17.
I have a theme that changes the background colour and main title text
however i cannot change the subtitle text colour
my style is shown here

<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
</style>

<style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
    <item name="android:background">#8B0000</item>
</style>

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

</style>

<style name="MyTheme.ActionBar.Subtitle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Subtitle">
    <item name="android:textColor">#FF0000</item>

</style>


what have a done wrong?

Upvotes: 17

Views: 13549

Answers (7)

Didier
Didier

Reputation: 1809

Step:

1) Include actionbar by id

2) Check if action bar exist (not null)

3) Check apk version

4) Change text color and background color of subtitle

Toolbar actionBarToolbar = (Toolbar) findViewById(R.id.action_bar); //change R.id.action_bar with your action bar id

       if (actionBarToolbar != null) // check if action bar is not null

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // check version of apk

         actionBarToolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary)); // change actionBarToolbar background color

         actionBarToolbar.setSubtitleTextColor(Color.WHITE); // finally  this line change subtitle text color

        }

Upvotes: 0

Devix
Devix

Reputation: 430

example add

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            android:background="@color/colorPrimary"
            app:titleTextColor="@color/white"
            app:subtitleTextColor="@color/white"
            android:gravity="center"
            >

subtitleColor

        app:subtitleTextColor="@color/white"

Upvotes: 1

Sai&#39;s Stack
Sai&#39;s Stack

Reputation: 1385

Add below code in your activity file

 actionBar.setSubtitle(Html.fromHtml("<font color='#FFBF00'>Here ActionBar Subtitle</font>"));

Upvotes: 3

StephenDonaldHuffPhD
StephenDonaldHuffPhD

Reputation: 958

This set of styles formats the action bar, including background, title and subtitle text; it also formats the tabs, including all states (selected, unselected, and all the focused/pressed combinations), as well as the tab text font and size (per Android developers instruction https://developer.android.com/training/basics/actionbar/styling.html):

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/themeActionBar</item>
    <item name="android:actionBarTabTextStyle">@style/styleActionBarTabText</item>
    <item name="android:actionMenuTextColor">@color/actionbar_text</item>
    <item name="android:actionBarTabStyle">@style/styleActionBarTabs</item>
    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/themeActionBar</item>
    <item name="actionBarTabTextStyle">@style/styleActionBarTabText</item>
    <item name="actionMenuTextColor">@color/actionbar_text</item>
    <item name="actionBarTabStyle">@style/styleActionBarTabs</item>
</style>

<!-- ActionBar styles -->
<style name="themeActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@drawable/abactionbargradient</item>
    <item name="android:titleTextStyle">@style/styleActionBarTitleText</item>
    <item name="android:subtitleTextStyle">@style/styleActionBarSubTitleText</item>
    <!-- Support library compatibility -->
    <item name="background">@drawable/abactionbargradient</item>
    <item name="titleTextStyle">@style/styleActionBarTitleText</item>
    <item name="subtitleTextStyle">@style/styleActionBarSubTitleText</item>
</style>

<!-- ActionBar title text -->
<style name="styleActionBarTitleText"
    parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/actionbar_title_text</item>
    <!-- The textColor property is backward compatible with the Support Library -->
</style>

<!-- ActionBar title text -->
<style name="styleActionBarSubTitleText"
    parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
    <item name="android:textColor">@color/actionbar_sub_title_text</item>
    <!-- The textColor property is backward compatible with the Support Library -->
</style>

<!-- ActionBar tabs text -->
<style name="styleActionBarTabText"
    parent="@style/Widget.AppCompat.ActionBar.TabText">
    <item name="android:textColor">@color/actionbar_text</item>
    <item name="android:textSize">10dp</item>
    <!-- The textColor property is backward compatible with the Support Library -->
</style>

<!-- ActionBar tabs styles -->
<style name="styleActionBarTabs"
    parent="@style/Widget.AppCompat.ActionBar.TabView">
    <!-- tab indicator -->
    <item name="android:background">@drawable/abactionbartabs</item>

    <!-- Support library compatibility -->
    <item name="background">@drawable/abactionbartabs</item>
</style>

This set of styles looks especially nice when the drawables are gradients using some set of coherent color values (i.e. lavender to purple for selected and purple to lavender for unselected, changing the values to darker for pressed and lighter for focused).

Upvotes: 3

MemoryLeak
MemoryLeak

Reputation: 502

Almost there. Here is the code:

<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
</style>

<style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
    <item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.Subtitle</item>
    <item name="android:background">#8B0000</item>
</style>

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

<style name="MyTheme.ActionBar.Subtitle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Subtitle">
    <item name="android:textColor">#FF0000</item>
</style>

Upvotes: 2

Hector
Hector

Reputation: 5356

I ended up with this (Its closer to what i need)

@style/Widget.Styled.ActionBar @style/Widget.Styled.ActionBar

<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.ActionBar.Solid">
    <item name="subtitleTextStyle">@style/TextAppearance.Sherlock.Widget.Styled.ActionBar.Subtitle</item>
    <item name="android:subtitleTextStyle">@style/TextAppearance.Sherlock.Widget.Styled.ActionBar.Subtitle</item>
    <item name="android:background">#8B0000</item>
</style>

<style name="TextAppearance.Sherlock.Widget.Styled.ActionBar.Subtitle" parent="Widget">
    <item name="android:textSize">12sp</item>
</style>

<style name="OverFlow" parent="Widget.Sherlock.ActionButton.Overflow">
    <item name="android:src">@drawable/abs__ic_menu_moreoverflow_holo_light</item>
</style>


Upvotes: 0

Androiderson
Androiderson

Reputation: 17083

Just change titleTextStyle to subtitleTextStyle

<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
  </style>

  <style name="MyTheme.ActionBarStyle"parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
  </style>

  <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/red</item>
  </style>

Upvotes: 21

Related Questions