According to the docs, I only need to set android:forceDarkAllowed=true
in my activity manifest and inherit theme from parent="Theme.MaterialComponents.DayNight"
. I tried that, but it didn't work.
Here's my manifest file:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:forceDarkAllowed="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And here's my styles.xml
styles:
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
...
</style>
<style name="AppTheme.Dark" parent="Theme.MaterialComponents.DayNight">
...
</style>
I tried to get the theme name of the activity by using this code below:
resources.getResourceName(
packageManager.getPackageInfo(packageName, PackageManager.GET_META_DATA)
.applicationInfo.theme
)
It shows me com.example.name:style/AppTheme
and not AppTheme.Dark
. How can I make it so that when I run the application, the MainActivity
automatically sets itself to use AppTheme.Dark
(i.e. dark mode) using android:forceDarkAllowed
?
That is not how dark mode works on Android 10.
First, Android does not magically append .Dark
to your theme at any point, regardless of whether or not the user enabled dark mode. You have android:theme="@style/AppTheme"
. So, Android will use AppTheme
all the time, and your AppTheme.Dark
will not be used.
Second, the idea behind DayNight
is that you use that as you base theme all the time, and Android will switch between normal and dark modes based on user request. So, if you switch AppTheme
to extend DayNight
, and get rid of AppTheme.Dark
, you will be matching what the documentation calls for.
android:forceDarkAllowed
is for cases where you are not in position to use a DayNight
theme, and you want Android to try to automatically convert your UI to a dark theme. If you wanted to keep AppTheme
extending Light
instead of DayNight
, this might work (I personally have not tried it with a MaterialComponents
theme). But, even then, it will only be when the user has enabled dark mode (e.g., tapped on the notification shade tile).
For example, in this sample app, I use DayNight
for my theme:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
I have different definitions for those colors in res/values/
and res/values-night/
. I do not use forceDarkAllowed
. And, when the user toggles night mode, my UI goes from:
to:
How can I make it so that when I run the application, the MainActivity automatically sets itself to use AppTheme.Dark (i.e. dark mode) using android:forceDarkAllowed?
You don't.
If you want a dark theme all the time, use a regular theme (not Light
, not DayNight
).