androidandroid-actionbarandroid-appcompatandroid-darkmode

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.


Solution

  • 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>