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
?
Open System Settings. Navigate to Developer Options. Scroll down the Hardware accelerated rendering section and enable the Override force-dark/Force dark mode* option.
On the mobile app, open Apps Settings” from your profile picture. On Android, tap on your profile picture, then on Settings, and then General. There, choose Appearance and tap the checkbox next to Dark theme.
Now go to Developer Option and find a setting called Override force-dark or Force Dark Mode. Tap on the toggle to enable it. This will force all apps to run in dark mode. Some apps will work quite well with dark mode enabled, while some apps may have mismatched color schemes, icons, and fonts.
In Android Studio, open themes. xml (app > res > values > themes > themes.
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With