312

how can I change the text color of the ActionBar? I've inherited the Holo Light Theme, I'm able to change the background of the ActionBar but I don't find out what is the attribute to tweak to change the text color.


Ok, I'm able to change the text color with the attribute android:textColorPrimary but it also changes the text color of the dropdown menu displayed when an overflow happen on the ActionBar buttons. Any idea how to change the color of those dropdown menu / List ?

Ravi
  • 34,851
  • 21
  • 122
  • 183
rnoway
  • 8,571
  • 4
  • 19
  • 9

28 Answers28

458

Ok, I've found a better way. I'm now able to only change the color of the title. You can also tweak the subtitle.

Here is my styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <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>
  </style>

  <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/red</item>
  </style>
</resources>
blbaker
  • 406
  • 3
  • 19
rnoway
  • 8,571
  • 4
  • 19
  • 9
  • 34
    This is the correct way. However, for consistency, you should be inheriting from `Widget.Holo.Light.ActionBar` for `MyTheme.ActionBarStyle`. – Jake Wharton Jun 21 '11 at 20:57
  • How do you change the background colour in addition to the text? I want the holo light theme with a black background action bar with white text and the blue underline as well... – topwik Aug 27 '12 at 14:42
  • 7
    Won't build - ADT complains that it requires v13 or above (so ... won't work on most Android handsets :( ) – Adam Feb 15 '13 at 12:31
  • How to inherit for ActionBarSherlock? – DroidLearner Mar 18 '13 at 06:08
  • 8
    @Adam I had the same problem, but the next answer works for me -- instead of parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title" use parent="@android:style/TextAppearance" – bkurzius Mar 20 '13 at 13:09
  • 9
    If you are using Appcompat library, replace Holo with AppCompat. It works – sha Sep 05 '13 at 13:25
  • 4
    @LOG_TAG Use Widget.AppCompat.Light.ActionBar – sha Oct 15 '13 at 09:40
  • Is it possible to change it programatically? – Bagusflyer Jan 24 '14 at 07:01
  • 2
    Doesn't work at all. Only change the action bar colour. Instead of changing the text colour. – Bagusflyer Feb 19 '14 at 09:51
  • 5
    @sha, correct ! Just to be precise, use "@style/TextAppearance.AppCompat.Widget.ActionBar.Title" and not "@android:style/TextAppearance.AppCompat.Widget.ActionBar.Title" – Mihir Shah Sep 04 '15 at 06:23
  • 1
    for those who are saying that they were unable to get it working in appcompat. here is the correct way to do it. http://prnt.sc/eosf3y – Jamshaid K. Mar 26 '17 at 20:55
  • 2
    Finally worked after setting name="titleTextStyle" instead of name="android:titleTextStyle" in MyTheme.ActionBarStyle – Eliseo Ocampos Jun 02 '17 at 17:20
102

I found a way that works well with any flavor of ActionBar (Sherlock, Compat, and Native):

Just use html to set the title, and specify the text color. For example, to set the ActionBar text color to red, simply do this:

getActionBar()/* or getSupportActionBar() */.setTitle(Html.fromHtml("<font color=\"red\">" + getString(R.string.app_name) + "</font>"));

You can also use the red hex code #FF0000 instead of the word red. If you are having trouble with this, see Android Html.fromHtml(String) doesn't work for <font color='#'>text</font>.


Additionally, if you want to use a color resource, this code can be used to get the correct HEX String, and removing the alpha if needed (the font tag does not support alpha):

int orange = getResources().getColor(R.color.orange);
String htmlColor = String.format(Locale.US, "#%06X", (0xFFFFFF & Color.argb(0, Color.red(orange), Color.green(orange), Color.blue(orange))));
Community
  • 1
  • 1
Phil
  • 35,852
  • 23
  • 123
  • 164
  • This gives an error because you have to put a string in the method Html.fromHtml(), and you are placing multiple parameters in the method. When replacing the "," by "+" this works perfectly :-). – pinyin_samu Nov 03 '13 at 19:28
  • @pinyin_samu, thanks for the catch! I had removed a `buildString` method that uses comma-separated Objects, and overlooked this. It is now fixed. – Phil Nov 04 '13 at 13:59
  • 3
    After decades of searching, this is the easiest solution! Great job! – bmkay Dec 03 '13 at 22:15
  • Most important thing; it works all the way down to Android API level 9! Awesome! – kramer65 Dec 23 '13 at 13:49
  • What if I don't have the color name, rather color code stored in a string? e.g. dynamically I received color code #004911 and stored in 'color' string and now trying to change the title color? – MSIslam Jan 03 '14 at 23:29
  • 2
    @MSI, you can use hex values too, just replace `red` with `" + color + "`. – Phil Jan 05 '14 at 01:42
  • @Phil : I tried in this way actually, doesn't work. Like my color string holds the code gree, here color = "#004911". I am applyng in this way: getSupportActionBar().setTitle(Html.fromHtml("" + getString(R.string.menu) + "")); I am using v7-appcompat library. My title changes, but the color doesn't. – MSIslam Jan 06 '14 at 18:20
  • In addition, could you please also tell how to change the color of back navigation arrow in the actionbar? – MSIslam Jan 06 '14 at 18:25
  • 1
    @MSI, can you try what [this post](http://stackoverflow.com/questions/6400619/android-html-fromhtmlstring-doesnt-work-for-font-color-text-font) suggests (also updated my question)? Just replace `#FF0000` with `"+color+"`. – Phil Jan 06 '14 at 18:27
  • @MSI that is an entirely different question that should not be asked in comments. Nonetheless, here is the link you need: http://stackoverflow.com/questions/9252354/how-to-customize-the-back-button-on-actionbar – Phil Jan 06 '14 at 18:34
  • 1
    Thank you Phil. It's working perfect! I tried in this way: getSupportActionBar().setTitle(Html.fromHtml(""+"Home"+"")); Just has to use ' in place of \. – MSIslam Jan 06 '14 at 18:55
  • @Phil It says that `call requires API 11 or above.` – Kraken Sep 20 '14 at 06:37
  • @Phil I had to use getSupportActionBar(). Thanks alot. – Kraken Sep 20 '14 at 06:41
  • @Phil - I am working on Xamarin Android and I tried same code for xamarin but it gives error that "Cannot convert Android.Text.ISpanned to int". Any idea ? – Ajay Sharma Nov 04 '15 at 05:12
  • On modern systems: `HtmlCompat.fromHtml("" + "v${BuildConfig.VERSION_NAME}!" + "", HtmlCompat.FROM_HTML_MODE_LEGACY)` – xjcl Jul 06 '20 at 21:03
95

I was having the same problem as you, it's a Holo.Light theme but I wanted to style the ActionBar color, so I needed to change the text color as well and also both Title and Menus. So at the end I went to git hub and looked at source code until I find the damn correct style:

<?xml version="1.0" encoding="utf-8"?>
<!-- For honeycomb and up -->
<resources>

    <style name="myTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/myTheme.ActionBar</item>
        <item name="android:actionMenuTextColor">@color/actionBarText</item>
    </style>

    <style name="myTheme.ActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@drawable/actionbarbground</item>
        <item name="android:titleTextStyle">@style/myTheme.ActionBar.Text</item>
    </style>

    <style name="myTheme.ActionBar.Text" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/actionBarText</item>
    </style>

</resources>

so now all you have to do is set whatever @color/actionBarText and@drawable/actionbarbground you want!

Piyush Kukadiya
  • 1,880
  • 16
  • 26
Budius
  • 39,391
  • 16
  • 102
  • 144
68

The ActionBar ID is not available directly, so you have to do little bit of hacking here.

int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if (actionBarTitleId > 0) {
    TextView title = (TextView) findViewById(actionBarTitleId);
    if (title != null) {
        title.setTextColor(Color.RED);
    }
}
mjama
  • 2,650
  • 2
  • 22
  • 24
Abhishek Chauhan
  • 1,984
  • 16
  • 16
  • 2
    `NullPointerException` on this line `actionBarTextView.setTextColor(Color.RED);` – DroidLearner Mar 18 '13 at 05:53
  • 3
    In case you're running API level < 11, ActionBarSherlock and you'd like to save yourself an expensive call to `getIndentifier()`, you can look in `(TextView) findViewById(com.actionbarsherlock.R.id.abs__action_bar_title);` first (be sure to check if it's not null). – Andre Apr 22 '13 at 08:36
  • "The ActionBar ID is not available directly" actually in my case the toolbar is easily accessible like `Toolbar toolbar = findViewById(R.id.toolbar);` where in the xml layout file it is surrounded by `com.google.android.material.appbar.AppBarLayout` element, then `toolbar.setTitleTextColor( getResources().getColor( R.color.colorViolet ) );` – YoussefDir Apr 25 '20 at 18:59
37

The most straight-forward way is to do this in the styles.xml.

Google's template styles.xml currently generates the following:

<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/colorAccent</item>
</style>

If you add one more line before the closing tag, as shown, that will change the text color to what it should be with a Dark ActionBar:

<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/colorAccent</item>
    <item name="actionBarTheme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>

If you want to customize the color to something else, you can either specify your own color in colors.xml or even use a built-in color from Android using the android:textColorPrimary attribute:

<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/colorAccent</item>
    <item name="actionBarTheme">@style/AppTheme.AppBarOverlay</item>
</style>


<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@android:color/darker_gray</item>
</style>

Note: This changes the color of the title and also the titles of any MenuItems displayed in the ActionBar.

Sam
  • 1,009
  • 10
  • 13
36

It's an old topic but for future readers, using ToolBar makes everything very easy:

Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
toolbar.setTitle(R.string.app_name);
toolbar.setTitleTextColor(getResources().getColor(R.color.someColor));
setSupportActionBar(toolbar);
Masoud Dadashi
  • 1,044
  • 10
  • 11
  • 1
    This works great for me. You can also use: toolbar.setTitleTextColor(Color.parseColor("#ffffff")) – alexrnov Sep 16 '20 at 08:01
29

Add it to the root of the action bar. I had this problem.

<style name="ActionBar" parent="@style/Theme.AppCompat.Light">
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <item name="actionMenuTextColor">@color/stdDarkBlueText</item>
</style>

<style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="titleTextStyle">@style/ActionBarTitleText</item>
    <item name="subtitleTextStyle">@style/ActionBarSubTitleText</item>
</style>

<style name="ActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/stdDarkBlueText</item>
    <item name="android:textSize">12sp</item>
</style>

<style name="ActionBarSubTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
    <item name="android:textColor">@color/stdDarkBlueText</item>
    <item name="android:textSize">12sp</item>
</style>

actionMenuTextColor - changes text color for the action bar text, it never worked when it was part of the Widget.Styled.ActionBar, so I had to add it to the root. Other 2 attributes change title and subtitle of an action bar.

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Nabdreas
  • 3,327
  • 4
  • 27
  • 30
29

i have done with simple one line code

actionBar.setTitle(Html.fromHtml("<font color='#ff0000'>ActionBartitle </font>"));
MilapTank
  • 9,988
  • 7
  • 38
  • 53
19

Setting a HTML string on the action bar doesn't work on the Material theme in SDK v21+

If you want to change it you should set the primary text color in your style.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Material.Light">
        <!-- Customize your theme here. -->
        <item name="android:textColorPrimary">@color/actionbar-text-color</item>
    </style>
</resources>    
Koen Hendriks
  • 599
  • 4
  • 10
13

If you want to style the subtitle also then simply add this in your custom style.

<item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>

People who are looking to get the same result for AppCompat library then this is what I used:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomActivityTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
        <!-- other activity and action bar styles here -->
    </style>

    <!-- style for the action bar backgrounds -->
    <style name="MyActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:background">@drawable/actionbar_background</item>
        <item name="background">@drawable/actionbar_background</item>
        <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
        <item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
        <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
        <item name="subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
     </style>
    <style name="MyTheme.ActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/color_title</item>
      </style>
</resources>
Community
  • 1
  • 1
Tarun
  • 13,727
  • 8
  • 42
  • 57
  • I'm trying for a light theme, dark action bar, and white text. Your code is the only thing I've found so far that has any effect at all, but it turns the whole action bar white. Any idea what might be going on? Yes I know this is a year and a half old. :-) – nasch May 16 '15 at 00:12
  • This helped me solve my problem. I just needed to add both `` _and_ `` because I was using the appcompat support library. – Rock Lee Sep 01 '15 at 06:48
  • 1
    the title disapear – jairhumberto Dec 24 '19 at 06:52
11

A lot of answers are deprecated so I'll update the thread and show how to change text color and backgroundcolor on ActionBar (Toolbar) and in ActionBar pop up menu.

The latest approach in Android (as of May 2018) in working with Action bar (Toolbar) is to use Theme with NoActionBar and use Toolbar instead.

so here is what you need:

  1. In Activity (which extends AppCompatActivity) declare Toolbar:

    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);
    
  2. Add Toolbar in the Activity's layout xml. Here we will set our custom (overwritten themes), note android:theme="@style/ThemeOverlay.AppTheme.ActionBar" and app:popupTheme="@style/ThemeOverlay.AppTheme.PopupMenu", they will do the trick.

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppTheme.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppTheme.PopupMenu"
    app:layout_constraintTop_toTopOf="parent" />
    
  3. In your styles.xml you will have to overwrite those two styles to set custom colors:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionBarTheme">@style/ThemeOverlay.AppTheme.ActionBar</item>
    <item name="actionBarPopupTheme">@style/ThemeOverlay.AppTheme.PopupMenu</item>
    </style>
    
    <style name="ThemeOverlay.AppTheme.ActionBar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@color/action_bar_text_color</item>
    <item name="android:background">@color/action_bar_bg_color</item>
    </style>
    
    <style name="ThemeOverlay.AppTheme.PopupMenu" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:background">@color/popup_bg_color</item>
    <item name="android:textColorPrimary">@color/popup_text_color</item>
    </style>
    

That's it folks

p.s. if you ask me I would say: YES, this whole Theme thing is a hell of a mess.

Kirill Karmazin
  • 6,256
  • 2
  • 54
  • 42
10

Found the way to do it nicely without creating your own layout on API >= 21.

It will only colorize texts and control drawables inside the action bar.

Hope it will be useful for someone.

<!--Material design primary colors-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:navigationBarColor">@color/primary_dark</item>
    <item name="actionBarTheme">@style/AppBaseTheme.Toolbar</item>
</style>

<!--Action bar-->
<style name="AppBaseTheme.Toolbar" parent="Widget.AppCompat.ActionBar.Solid">
    <item name="android:textColorPrimary">@color/action_bar_text</item>
    <item name="colorControlNormal">@color/action_bar_text</item>
</style>
reaper
  • 398
  • 3
  • 15
8

These functions work well

In Java

private void setActionbarTextColor(ActionBar actBar, int color) {

    String title = actBar.getTitle().toString();
    Spannable spannablerTitle = new SpannableString(title);
    spannablerTitle.setSpan(new ForegroundColorSpan(color), 0, spannablerTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    actBar.setTitle(spannablerTitle);

}

then to use it just feed it your action bar and the new color i.e.

ActionBar actionBar = getActionBar();    // Or getSupportActionBar() if using appCompat
int red = Color.RED
setActionbarTextColor(actionBar, red);

In Kotlin

You can use an extension function like this:

private fun ActionBar.setTitleColor(color: Int) {
    val text = SpannableString(title ?: "")
    text.setSpan(ForegroundColorSpan(color),0,text.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
    title = text
}

And then apply to your ActionBar with

actionBar?.setTitleColor(Color.RED)
Antoine
  • 307
  • 2
  • 13
365SplendidSuns
  • 3,175
  • 1
  • 21
  • 28
5

This is the solution I used after checking all answers

<!-- Base application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorAccent">@color/Button_color</item>
    <item name="android:editTextStyle">@style/EditTextStyle</item>
    <item name="android:typeface">monospace</item>
    <item name="android:windowActionBar">true</item>
    <item name="colorPrimary">@color/Title_Bar_Color</item>
    <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>

</style>

<style name="EditTextStyle" parent="Widget.AppCompat.EditText"/>

<style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="titleTextStyle">@style/ActionBarTitleText</item>
    <item name="android:background">@color/Title_Bar_Color</item>
    <item name="background">@color/Title_Bar_Color</item>
    <item name="subtitleTextStyle">@style/ActionBarSubTitleText</item>
</style>

<style name="ActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/solid_white</item>
    <item name="android:textSize">12sp</item>
</style>

<style name="ActionBarSubTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
    <item name="android:textColor">@color/solid_white</item>
    <item name="android:textSize">12sp</item>
</style>

Change the required colors it will work

Smaran
  • 1,651
  • 2
  • 11
  • 15
5

Try adding this in your Activity's onCreate. Works on almost every Android version.

 actionBar.setTitle(Html.fromHtml("<font color='#ffff00'>Your Title</font>"));  

or

getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffff00'>Your Title</font>"));
onexf
  • 3,674
  • 3
  • 22
  • 36
  • You should put a string like this: String coloredTitle = "" + Your_Title + ""; Html.fromHtml(coloredTitle); – Tincho825 Jan 28 '19 at 16:20
3

just put this in your thme style.

<item name="android:textColorPrimary">@color/yourcolor</item>

Ali Keb
  • 43
  • 2
2

Try a lot of methods, in the low version of the API,a feasible method is <item name="actionMenuTextColor">@color/your_color</item> and don't use the Android namespace,hope can help you

ps:

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionMenuTextColor">@color/actionMenuTextColor</item>
</style>
BoBoMEe
  • 35
  • 2
2

The most simple way is :
Add these two lines to your styles.xml file

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:textColorSecondary">@color/white</item>
<item name="android:textColor">@color/white</item>
</style>

Replace the color with your color.

Ali Najafi
  • 21
  • 1
  • i have material theme and these lines are not working here is my code ``` ``` – Shoaib Kakal Sep 10 '20 at 08:26
1

Here Style.xml is like

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


 <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <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/colorBlack</item>
</style>

You should add

<item name="actionBarTheme">@style/MyTheme</item> 

in AppTheme

Arjun Othayoth
  • 351
  • 3
  • 5
1

In my case, I switched to AndroidX Toolbar instead of using the native action bar. Also, I rely on view binding to access the toolbar from the activity, but you can use findViewById(...) for that.

(I removed the code which is irrelevant).

styles.xml:

...
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
  ...
</style>
...

activity_main.xml:

...
<androidx.appcompat.widget.Toolbar
  android:id="@+id/toolbarMain"
  android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
  app:titleTextColor="@color/colorOnPrimary"
  ...
/>
...

MainActivity.kt:

class MainActivity : AppCompatActivity() {

  private lateinit var binding: ActivityMainBinding
  ...

  override fun onCreate(savedInstanceState: Bundle?) {
    ...
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
    setSupportActionBar(binding.toolbarMain)
    ...
  }

  ...
}
iloo
  • 926
  • 12
  • 26
0

A nice solution is to user a SpannableStringBuilder and set the color that way. You can even use different colors on different parts of the string, add images etc.

Tested with the new support library.

See Android: Coloring part of a string using TextView.setText()?

Community
  • 1
  • 1
Maciej Swic
  • 11,139
  • 8
  • 52
  • 68
0

This is not the recommended solution as I am going in android apis here but as my application requires to change the theme dynmically on conditions xml not possible here, So I need to do this. But This solution is working very nice.

Solution:--

 /**
 * 
 * @author Kailash Dabhi
 * @email kailash09dabhi@gmail.com
 *
 */ 
 public static void setActionbarTextColor(Activity activity, int color) {
    Field mActionViewField;
    try {
        mActionViewField = activity.getActionBar().getClass()
                .getDeclaredField("mActionView");
        mActionViewField.setAccessible(true);
        Object mActionViewObj = mActionViewField.get(activity
                .getActionBar());

        Field mTitleViewField = mActionViewObj.getClass().getDeclaredField(
                "mTitleView");
        mTitleViewField.setAccessible(true);
        Object mTitleViewObj = mTitleViewField.get(mActionViewObj);

        TextView mActionBarTitle = (TextView) mTitleViewObj;
        mActionBarTitle.setTextColor(color);
        // Log.i("field", mActionViewObj.getClass().getName());
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }

}
Kailash Dabhi
  • 3,473
  • 1
  • 29
  • 47
0

For Android 5 (lollipop) you will have to use android:actionBarPopupTheme to set the textColor for the overflow menu.

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
0

you can change color of any text by use html <font> attribute directly in xml files. for example in strings.xml :

<resources>
    <string name = "app_name">
        <html><font color="#001aff">Multi</font></html>
        <html><font color="#ff0044">color</font></html>
        <html><font color="#e9c309">Text </font></html>
    </string>
</resources>

enter image description here

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
hofs
  • 469
  • 4
  • 12
0
<android.support.v7.widget.Toolbar
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/color_primary"
  android:theme="@style/GalaxyZooThemeToolbarDarkOverflow"
  app:popupTheme="@style/Theme.AppCompat.NoActionBar" />


<style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
  <!-- android:textColorPrimary is the  color of the title text
       in the Toolbar, in the Theme.AppCompat theme:  -->
  <item name="android:textColorPrimary">@color/abc_primary_text_material_light</item>
 
  <!-- android:textColorPrimaryInverse is the  color of the title
       text in the Toolbar, in the Theme.AppCompat.Light theme:  -->
  <!-- <item name="android:textColorPrimaryInverse">@color/abc_primary_text_material_light</item> -->
 
  <!-- android:actionMenuTextColor is the color of the text of
        action (menu) items in the Toolbar, at least in the
        Theme.AppCompat theme.
        For some reason, they already get the textColorPrimary
        when running on API 21, but not on older versions of
        Android, so this is only necessary to support older
        Android versions.-->
        <item name="actionMenuTextColor">@color/abc_primary_text_material_light</item>
  <!-- android:textColorSecondary is the color of the menu
       overflow icon (three vertical dots) -->
  <item name="android:textColorSecondary">@color/abc_secondary_text_material_light</item>
 
  <!-- This would set the toolbar's background color,
        but setting this also changes the popup menu's background,
        even if we define popupTheme for our <Toolbar> -->
  <!-- <item name="android:background">@color/color_primary</item> -->
</style>


**Reference:**
[https://www.murrayc.com/permalink/2014/10/28/android-changing-the-toolbars-text-color-and-overflow-icon-color/][1]
0

If you need to set the color programmatically this is the way to do it:

static void setActionBarTextColor(Activity activity, int color)
{
      ActionBar actionBar = activity instanceof AppCompatActivity
                    ? ((AppCompatActivity) activity).getSupportActionBar()
                    : activity.getActionBar();
      String title = activity.getTitle(); // or any title you want
      SpannableString ss = new SpannableString(title);
      ss.setSpan(new ForegroundColorSpan(color), 0, title.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
      actionBar.setTitle(ss);
}
TBog
  • 23
  • 4
0

We need to define the theme in our toolbar with app:theme attribute like the below code snippet.

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/AppTheme.ToolbarFont" />

Before that, we have to add the theme in our styles.xml file and we have to define font-family in that style like the below code snippet.

<style name="AppTheme.ToolbarFont" parent="AppTheme">
<!--This line changes the color of text in Toolbar-->
<item name="android:textColorPrimary">@color/black</item>
<!--This line changes the color of icons in toolbar (back, overflow menu icons)-->
<item name="android:textColorSecondary">@color/azul</item>
<item name="textAllCaps">false</item>
<item name="android:textSize">16sp</item>
<item name="android:fontFamily">@font/montserrat_semi_bold</item>

To add custom fonts we need to create a folder with the name “font” in the res directory like the below screenshot.

enter image description here

We have achieved a custom font in our toolbar.

enter image description here

0

If you are using a custom action bar you can use these properties:

app:titleTextColor="@color/...."
app:subtitleTextColor="@color/...." 
or by a method like this:
setTitleTextColor()

for more properties check this link

Marawan Mamdouh
  • 584
  • 1
  • 6
  • 15