This section describes how to support the dark theme (dark mode) app, which is fully supported from Android 10.
res/values/styles.xml First of all, update styles.xml, which is also used for normal theme customization, and define the color you want to switch when dark mode is ON / OFF.
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.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="android:colorBackground">@color/colorBackground</item>
<item name="android:textColor">@color/textColor</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
<item name="android:textColorSecondary">@color/textColorSecondary</item>
</style>
</resources>
Describe the color definition referenced when dark mode is enabled in the system settings in res / values-night / colors.xml, and the normal color in res / values / colors.xml.
<resources>
<color name="colorPrimary">#212121</color>
<color name="colorPrimaryDark">#212121</color>
<color name="colorAccent">#80cbc4</color>
<color name="colorTransparent">#00000000</color>
<color name="textColor">#FFFFFF</color>
<color name="textColorPrimary">#FFFFFF</color>
<color name="textColorSecondary">#808080</color>
<color name="colorBackground">#313131</color>
<color name="colorCardBackground">@color/colorPrimary</color>
<color name="colorBackgroundBottomAppBar">#353535</color>
</resources>
If you support up to this point, the display will switch according to the system settings without writing any code.
Use AppCompatDelegate.setDefaultNightMode to apply to the entire app.
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES); //Applies to the entire app
AppCompatDelegate.setLocalNightMode(MODE_NIGHT_YES); //Applies only to specific activities
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO); //Applies to the entire app
AppCompatDelegate.setLocalNightMode(MODE_NIGHT_NO); //Applies only to specific activities
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM); //Applies to the entire app
AppCompatDelegate.setLocalNightMode(MODE_NIGHT_FOLLOW_SYSTEM); //Applies only to specific activities
Recommended Posts