[Android] How to deal with dark themes

Introduction

This section describes how to support the dark theme (dark mode) app, which is fully supported from Android 10.

Resource creation

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>

Creating colors.xml

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.

Example) res / values-night / 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.

If you want to switch dynamically with the settings in the app

Use AppCompatDelegate.setDefaultNightMode to apply to the entire app.

Always displayed with a dark theme

AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES); //Applies to the entire app
AppCompatDelegate.setLocalNightMode(MODE_NIGHT_YES);   //Applies only to specific activities

Always dark theme OFF (normal display)

AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO); //Applies to the entire app
AppCompatDelegate.setLocalNightMode(MODE_NIGHT_NO);  //Applies only to specific activities

Linked to system settings (this is the default unless otherwise specified)

AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM); //Applies to the entire app
AppCompatDelegate.setLocalNightMode(MODE_NIGHT_FOLLOW_SYSTEM);   //Applies only to specific activities

Recommended Posts

[Android] How to deal with dark themes
How to deal with Precompiling assets failed.
Android: How to deal with "Could not determine java version from '10 .0.1'"
Diet program with preprocessor (how to deal with i-appli size)
How to deal with fatal: remote origin already exists.
How to deal with No template for interactive request
[Docker environment] How to deal with ActiveSupport :: MessageEncryptor :: InvalidMessage
How to number (number) with html.erb
[Rails] How to deal with URL changes after render
How to update with activerecord-import
Common problems with WSL and how to deal with them
[Docker + Rails] How to deal with Rails server startup failure
How to take a screenshot with the Android Studio emulator
How to get values in real time with TextWatcher (Android)
How to make an app using Tensorflow with Android Studio
How to deal with the error ERROR: While executing gem ... (Gem :: FilePermissionError)
How to deal with Bundler :: Dsl :: DSLError by rewriting gemfile
How to fix Android apps crashing with RenderThread mystery error
How to deal with different versions of rbenv and Ruby
How to scroll horizontally with ScrollView
How to get started with slim
How to "hollow" View on Android
How to enclose any character with "~"
How to use mssql-tools with alpine
How to get along with Rails
[Android] How to make Dialog Fragment
How to start Camunda with Docker
[Rails / RSpec] How to deal with element has zero size error
How to deal with 405 Method Not Allowed error in Tomcat + JSP
Project facet Java version 13 is not supported. How to deal with
How to call with One Touch (without confirmation) on Android (Java)
Android: How to deal with "Cause: cannot find android.support.transition.R $ id 2: android.support.transition.R $ id found in android / support / transition / R $ id 2.class"
How to crop an image with libGDX
How to adjustTextPosition with iOS Keyboard Extension
How to share files with Docker Toolbox
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
How to detect microphone conflicts on Android
How to switch thumbnail images with JavaScript
[Note] How to get started with Rspec
How to do API-based control with cancancan
How to achieve file download with Feign
How to update related models with accepts_nested_attributes_for
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
How to handle sign-in errors with devise
How to delete data with foreign key
How to test private scope with JUnit
How to monitor nginx with docker-compose with datadog
How to achieve file upload with Feign
How to run Blazor (C #) with Docker
How to build Rails 6 environment with Docker
How to use ExpandableListView in Android Studio
Java8 / 9 Beginners: Stream API addiction points and how to deal with them
Challenge to deal with garbled characters with Java AudioSystem.getMixerInfo ()
It's a pain to deal with old dates
[Java] How to test for null with JUnit
How to mock each case with Mockito 1x
How to mock each case with PowerMock + Mockito1x