Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ActionBar text color after inheriting Theme.AppCompat.DayNight

I'm implementing support for dark mode. But after I changed Theme parent to Theme.AppCompat.DayNight, text in Action Bar is black (black in day mode and white in dark mode). I want the text to be always white. I tried to change text color in styles.xml (values and values-night)

<style name="AppTheme" parent="Theme.AppCompat.DayNight">
  <item name="android:actionBarStyle">@style/MyActionBarStyle</item>
</style>

<style name="MyActionBarStyle" parent="Theme.AppCompat.DayNight.DarkActionBar">
  <item name="android:titleTextStyle">@style/MyActionBarTitleTextStyle</item>
</style>

<style name="MyActionBarTitleTextStyle" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
  <item name="android:textColor">@color/white</item>
</style>

but it is not working. The title text color in ActionBar is still black.

like image 411
MagogCZ Avatar asked Nov 01 '19 18:11

MagogCZ


People also ask

How to change text color of action bar in android studio?

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.

How can I change action bar title color?

xml in values folder this will change your action bar color.. Replace #666666 with your selected color code for title background color and replace #000000 for your title text color.


1 Answers

Using the ActionBar in your theme you should use the actionBarStyle attribute:

<style name="AppTheme" parent="Theme.AppCompat.DayNight">
   <item name="actionBarStyle">@style/....</item>
</style>

<style name="MyActionBarStyle" parent="Theme.AppCompat.DayNight.DarkActionBar">
  <item name="titleTextStyle">@style/...</item>
</style>

If you are using the Toolbar API, you can also use

 <Toolbar
   app:titleTextColor="@color/...."
   ../>

Instead with the Material Components Library and the Toolbar API you can use:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
   <item name="toolbarStyle">@style/my_Toolbar</item>
</style>

<style name=my_Toolbar" parent="@style/Widget.MaterialComponents.Toolbar">
    <item name="titleTextColor">@color/...</item>
</style>    
like image 142
Gabriele Mariotti Avatar answered Oct 01 '22 18:10

Gabriele Mariotti