2

I'm a bit puzzled. I'm using a DayNight theme in my app (using AppCompatDelegate.setDefaultNightMode()), but can't get it to work in my MainActivity. The MainActivity (which extends FragmentActivity) looks like it's never set to dark theme - it always remains in light theme.

I tried setting the theme directly in my MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    super.onCreate(savedInstanceState);
    // create main activity.
}

But this is not working.

I have set all of the colors in my layout files properly using ?attr/colorReference. Does anyone know what is going wrong here?

EDIT: My styles.xml is as follows:

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <!--Default typeface and colors:-->
    <item name="android:typeface">monospace</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorAccentDarker">@color/colorAccentDarker</item>
    <item name="colorAccentDarker_80percent">@color/colorAccentDarker_80percent</item>

    <!--Show people's own wallpaper background-->
    <item name="android:windowShowWallpaper">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>
Jorn Rigter
  • 745
  • 1
  • 6
  • 25

1 Answers1

5

MainActivity (which extends FragmentActivity)

FragmentActivity has no idea of AppCompat. AppCompatDelegate is only used by AppCompatActivity or you have to wire it manually to your other activities.

You can extend AppCompatActivity instead of FragmentActivity.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • `AppCompatActivity` extends `FragmentActivity`. – Eugen Pechanec Apr 16 '20 at 08:10
  • This works for me but introduced these side effects: an ugly title popping up and the transparent background of buttons no longer being transparent – riezebosch Jul 27 '21 at 15:02
  • @riezebosch 1. I suspect you set android:background in themes, don't do that, it's a widget/style attribute. You may want to google "themes vs styles android" and read up on the differences. 2. AppCompat button style is "buttonStyle" as opposed to plain Android's "android:buttonStyle". Many other widgets follow suit. Update your theme accordingly and it should work again. – Eugen Pechanec Jul 27 '21 at 15:06
  • please help with this device specific issue, if you have any idea: https://stackoverflow.com/questions/71333304/android-dark-light-mode-not-working-in-android – Mayura Devani Mar 03 '22 at 07:25