Android: How to switch theme for dark mode? - android

Since Android 10, you can switch between dark mode and default light mode. I didn't make any closer research yet on this since it's a new topic. Is Dark mode color switching automatic by OS or is there any way to tell my App to switch different app-theme if Dark mode is turned on? Also dark mode is possibility for some Android 9 devices.
Because I made custom Dark theme with custom parameters and I set dark color for each of my color in resources (using custom attributes in attrs.xml and applied specific color resource to them inside theme in styles.xml). Same it is working for different color schemes of my App (blue, red, green for example). That way I can tell which color would be used for different views in my App and not system.
Only thing I need is to detect if dark mode is on/off in system. I can force user to turn on Dark mode in App settings (custom settings) but theme can be affected by system dark mode (turned on in Phone settings).

From official documentations:
In order to support Dark theme, you must set your app's theme (usually found in res/values/styles.xml) to inherit from a DayNight theme:
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
You can also use MaterialComponents' dark theming:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
This ties the app's main theme to the system-controlled night mode flags and gives the app a default Dark theme (when it is enabled).
It means that if you use a DayNight theme, the OS itself handles the app theme. if you want to force app uses dark theme check this documentation.
UPDATE:
To detect the device theme:
switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
case Configuration.UI_MODE_NIGHT_YES:
…
break;
case Configuration.UI_MODE_NIGHT_NO:
…
break;
}

Related

Force disable dark mode in android application not working

I have force disable dark mode in my android application but still the app is in dark mode when my phone is in dark mode
I have also changed the theme for both light and night modes to light theme with no action bar but its not working
1)Add this line in each activity just below onCreate
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
OR
add this line in your themes under style section
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

Android - System doesn't recognize my theme as dark mode

I have two themes. One is:
<style name="ThemeDay" parent="Theme.AppCompat.Light.NoActionBar">
Other one is:
<style name="ThemeNight" parent="Theme.AppCompat.DayNight.NoActionBar">
I apply themes in every activity like this before super.onCreate()
if(GenelUtil.getNightMode()){
setTheme(R.style.ThemeNight);
}else{
setTheme(R.style.ThemeDay);
}
Theme is applied. But system doesnt behave like selected theme.
For example when system is light mode and app is dark mode, Navigation bar is still white, dialogs are still white etc..
But when I open instagram and switch to dark mode, system also behave like its in dark mode.
What do I do wrong here? How can I fix this?
DayNight theme is picking automatically dark or light params/colors basing on system settings (by default) or set/forced by you (e.g. using AppCompatDelegate.setDefaultNightMode())
when you have light theme in your system and you will use DayNight theme for your app then still not overriden colors will be same as in "light" theme

How to Implement Dark Mode Theme in Android?

Android Q launched by Google, Dark Theme was released with it. Dark Mode applies to both the system UI and the apps running in it.
I want to implement this feature to application, any good suggestions ?
Help will be appreciated !
Make sure you import the latest version of the google material library
com.google.android.material:material:version.latest
Then you can set your apps theme to Theme.MaterialComponents.DayNight....
This will switch your UI to a dark theme variant when the user toggles it in their system
https://developer.android.com/guide/topics/ui/look-and-feel/darktheme
In order to support Dark theme, you must set your app's theme (usually found in res/values/styles.xml) to inherit from a DayNight theme:
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
for more information

Select splash screen image and background color based on the application theme not the system(ignore MODE_NIGHT_FOLLOW_SYSTEM.)

I'm using DayNight theme for my android application, and I've made a values-night directory to customize my theme the way I want to, everything is working well, the problem is when I launch my application the splash screen color is chosen depending on the system theme.
let's say the system theme is light and my application is on the dark theme when launch my application the splash screen will be light them colors then the rest of the application will be in the dark theme, and if we change the application theme to light and the system theme to dark then when I launch my application the splash screen will be in the dark theme colors but the rest of the app will be in light theme.
I looked around and I found out that dayNight theme gets the default value for the application theme from the system theme, as you can read in the setDefaultNightMode descriptions
Sets the default night mode. This is used across all activities/dialogs but can be overridden locally via setLocalNightMode(int).
This method only takes effect for those situations where applyDayNight() works. Defaults to MODE_NIGHT_FOLLOW_SYSTEM.
This only takes effect for components which are created after the call. Any components which are already open will not be updated.
I was wondering if there is a way to change that default value? or change the source of the default value.

Set theme programatically to Android system preview window

I have an app in which the user can choose between the default theme and a dark theme. The manifest applies AppTheme to the <application>, while each activity checks the relevant flag and uses setTheme(AppTheme.Dark) if necessary.
This implementation works well for all my activities, but the preview window shown by the OS before the application launches uses the Manifest theme, i.e. the light one. So users get a sudden white screen before reaching the dark app.
Based on other SO answers, I gathered that the only customization we can do is to set a windowBackground drawable for the window to use (and a statusBarColor if needed).
Even though I have two different windowBackground set for each theme, I can't change the theme for the preview window since the Application class seems to load after it, and setting theme there makes no difference.
I'd like to avoid disabling the preview since it adds a noticeable delay without much indication that the app is launching. Is there any solution to this? I'm considering having a dark color for both themes in the worst case, like the Reddit app does.
Styles:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- primary and accent color attributes -->
<item name="android:windowBackground">#color/activityBackgroundLight</item>
</style>
<style name="AppTheme.Dark" parent="AppTheme">
<!-- primary and accent color attributes -->
<item name="android:windowBackground">#color/activityBackgroundDark</item>
</style>
Note: While I'm using colors here, I've already tried with a gradient drawable and it doesn't change anything.
UPDATE: I wasn't able to get any other method, so I went ahead and applied the dark theme by default on the SplashActivity. This means the preview window is always dark, while the check in the SplashActivity's onCreate is inverted (compared to other activities) and applies the light theme if needed.
Still looking for a solution that actually lets me switch at runtime.

Categories

Resources