Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppCompat DayNight theme not work on Android 6.0?

I am using the new Theme.AppCompat.DayNight added in Android Support Library 23.2

On Android 5.1 it works well.

On Android 6.0, activity looks like using light theme, but dialog looks using dark theme.

My Application class:

public class MyApplication extends Application {
    static {
        AppCompatDelegate.setDefaultNightMode(
                AppCompatDelegate.MODE_NIGHT_YES);
    }
}

My styles.xml

<style name="AppTheme" parent="Theme.AppCompat.DayNight">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="Dialog.Alert" parent="Theme.AppCompat.DayNight.Dialog.Alert"/>

My code to show a dialog:

new AlertDialog.Builder(mContext, R.style.Dialog_Alert)
                .setTitle("Title")
                .setMessage("Message")
                .show();
like image 514
Rikka Avatar asked Feb 27 '16 03:02

Rikka


People also ask

What is DayNight theme Android?

The content is correct as-of April 26th 2019. The DayNight functionality in AppCompat allows your app to easily switch between a dark ⚫ and light ⚪ theme. This has many benefits for your users, from saving power on OLED displays, to increasing usability for people with reduced-vision, and more.

What is theme AppCompat DayNight?

AppCompat. DayNight. DayNight theme allows to switch between light(day) and dark(night) themes, based on the time. Keep in mind that it supports API 14+. Any lower and it defaults to the Light theme.

What is AppCompatDelegate?

public abstract class AppCompatDelegate. This class represents a delegate which you can use to extend AppCompat's support to any android. app. Activity . When using an AppCompatDelegate , you should call the following methods instead of the android.


1 Answers

Google have fix it in support 23.2.1

Old answer:

On Android 6.0, system's night mode setting defalut is UiModeManager.MODE_NIGHT_NO, it will change Resources.Configuration.uiMode before onCreate is called. However, support library apply its night mode setting in onCreate in AppCompatActivity, it's too late, I think thats why it not work on 6.0.

So if we can Override getResources() in AppCompatActivity and change uiMode.

Old answer:

Here are code to fix not work on Android 6.0

public class Application extends android.app.Application {
    static {
        AppCompatDelegate.setDefaultNightMode(
                AppCompatDelegate.MODE_NIGHT_);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        // add this code for 6.0
        // DO NOT DO THIS. It will trigger a system wide night mode.
        // This is the old answer. Just update appcompat.
        // UiModeManager uiManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
        // uiManager.setNightMode(UiModeManager.MODE_NIGHT_);
    }
}

Note: If your app don't have location permission, your app will not have the same calculate result of system. It means it is possible support library thinks it is night now when system not, this will cause some of your UI looks dark some light.

The best way is wait for Google to fix it.

like image 96
Rikka Avatar answered Sep 18 '22 09:09

Rikka