18

I'm developing an app and I want to change the textcolor of the actionbar title. now, I defined a theme in my manifest:

<application
        android:theme="@style/AppTheme">
</application>

which is defined in styles.xml:

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

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

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

    <style name="AppTheme.TextView" parent="@android:style/Widget.TextView">
        <item name="android:textColor">#FFFFFF</item>
    </style>

this doesn't work but I don't get why. this should work according to this answer.neither the textview style is working.

what I am doing wrong?

UPDATE: changed to meet Grace Feng answer below

Community
  • 1
  • 1
jack_the_beast
  • 1,838
  • 4
  • 34
  • 67

13 Answers13

18

Using SpannableString we can set Text Color of Actionbar title

SpannableString s = new SpannableString(title);
s.setSpan(new ForegroundColorSpan(Color.GREEN), 0, title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setTitle(s);
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
9
<style name="AppTheme" parent="android:style/Theme.Holo.Light">

<style name="AppTheme.TextView" parent="android:Widget.TextView">

You missed a symbol "@" here?

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

also

<style name="AppTheme.TextView" parent="@android:Widget.TextView">
panda
  • 560
  • 2
  • 10
4

You could find how to customize ActionBar here. I recommend to use ToolBar and Theme.AppCompat instead - example.

Community
  • 1
  • 1
Vladimir Markeev
  • 654
  • 10
  • 25
4

I am assuming you are using ToolBar as ActionBar. Of all the answers, I think this is the easiest one to implement. And works on all API versions.

So here it is:

Action bar title uses textColorPrimary as its text color. So to change that in styles.xml create a new style with an item for textColorPrimary and set whatever color you need.

<style name="appBarTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:textColorPrimary">@color/colorRed</item>
</style>

Now, add that style as your theme for toolbar in the XML.

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_scrollFlags="enterAlways"
    android:id="@+id/toolbar"
    android:theme="@style/appBarTheme">
</android.support.v7.widget.Toolbar>

And now you got what you need.

Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
Akash Rudra
  • 136
  • 1
  • 2
  • `android:textColorPrimary` is very precise. I had to look at a lot of answers till I reached this and it worked. Thanks! – JorgeAmVF May 18 '19 at 16:08
2

Just add app:titleTextColor="@color/CUSTOM_COLOR" like so:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/yellow_light"
    app:titleTextColor="@color/CUSTOM_COLOR"
    app:popupTheme="@style/AppTheme.PopupOverlay"/>
mkierc
  • 1,193
  • 2
  • 15
  • 28
1

wandering around StackOverflow I found this answer which finally worked.

I report it here:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/ActionBar</item>
    ...
</style>

<style name="ActionBar" parent="@android:style/Widget.ActionBar">
  <item name="android:titleTextStyle">@style/ActionBar.Title</item>
  <item name="android:subtitleTextStyle">@style/ActionBar.Subtitle</item>
  ...
</style>

<style name="ActionBar.Title">
  <item name="android:textColor">@color/my_color</item>
</style>

<style name="ActionBar.Subtitle">
  <item name="android:textColor">@color/my_color</item>
</style>
Community
  • 1
  • 1
jack_the_beast
  • 1,838
  • 4
  • 34
  • 67
1

When i got the same problem, i used:

<item name="titleTextStyle">@style/ActionBarTextStyle</item>

<item name="android:titleTextStyle">@style/ActionBarTextStyle</item>
IDmikael
  • 477
  • 1
  • 6
  • 16
0

I think it is just the problem of you activity.I used the same code as you, and it works.enter image description here

Firstly ,i used AppCompatActivity as the base activity and get error. and then i use activity and it works. In my opinion, your code is use for theme for Theme.Holo, and now we use Theme.AppCompat . So if you want change, just reference Changing Background and text color of AppCompat Light DarkActionBar Theme on android. It works for me.Good luck for you.

Community
  • 1
  • 1
sunday
  • 48
  • 6
  • I have a NavigationDrawer in the activity, so it extends AbstractNavDrawerActivity. unfortunately I didn't write this code so I don't really know why the guy who written this used it this way :S – jack_the_beast Nov 16 '15 at 09:28
  • 1
    I see AbstractNavDrawerActivity and this activity is extended from ActionBarActivity. And your theme is form Theme.Holo . So it will not work. If you do not want to change the activity , just change the theme like http://stackoverflow.com/questions/29957822/changing-background-and-text-color-of-appcompat-light-darkactionbar-theme-on-and. it will work – sunday Nov 17 '15 at 01:56
0

Seems you've used style instead of theme. Styles and themes differ. So replace

<style name="AppTheme" parent="android:style/Theme.Holo.Light">

With

<style name="AppTheme" parent="android:Theme.Light">
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • if I put this like you suggested android studio is not able to find it and returns an error, all the suggestion include `:style` – jack_the_beast Nov 24 '15 at 11:02
  • I did. I understend I should not use it for the application tab but rather for sigle views. But then my questio remains, how do I change the text color? – jack_the_beast Nov 24 '15 at 13:05
0

I don't know the solution, but I was able to change the text color between white and black with this method.

so, in the styles.xml you have:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <item name="colorPrimary">#673AB7</item>
        <item name="colorPrimaryDark">#512DA8</item>
        <item name="colorAccent">#FF4081</item>
    </style>
</resources>

if you change what's inside the parent="..." to dark or light themes, it adapts the text color to these themes. For example, right now you have a dark theme, and the text is white, but when I change to "Theme.AppCompat.Light" the text color turns black.

Hope it helps in some way.

Max
  • 1
0

Change the Activity theme as:

<style name="ThemeName" parent="android:Theme.Holo.Light">

   <item name="android:actionMenuTextColor">@color/white</item>

</style>
Unheilig
  • 16,196
  • 193
  • 68
  • 98
coder
  • 312
  • 3
  • 12
0

Just had this problem, finally figured it out. In your style.xml do not specify a background color for your toolbar. Then define the color in each layout.xml file:

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#B678EE"
    android:elevation="4dp"
    android:theme="@style/ToolbarTheme"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

This gives you the flexibility to be able to easily have different action bar colors depending on the activity if need be as well.

jon.bray.eth
  • 527
  • 6
  • 13
0

This is a usable code. Follow this code.

<style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorApp</item>
</style>

<style name="TextAppearance.AppCompat.Widget.ActionBar.Title" parent="@android:style/TextAppearance">
    <item name="android:textSize">23dp</item>
    <item name="android:textColor">@color/YOUR_COLOR</item>
</style>

In second style tag we customize the actionBar title.

Now Add this above code in the application in Manifest File

<application
        android:theme="@style/AppTheme">
        <activity>  .....  </activity>
        <activity>  .....  </activity>
</application>