Simplest Way to apply Day/Night Theme Switching in Android

How you can do Flexible Switching of Day-Night Theme in ANDROID?

Because the Day/Night or Light/Dark theme is just about the new normal since Android 10 began moving out, the majority of the popular apps like Rocks Player Ultra HD Video have this selection. If you value to test new add-ons in your apps, it’s time for you to try it out making your application future-proof with this particular feature.

We will describe how you can implement day/night styles in your application in the simplest way possible, with AndroidX’s AppCompat library. Prior to getting began, bare this factor in your mind – we highly recommend AndroidX libraries if you’re focusing on a brand new project. You may also transfer your present project to Android should you haven’t tried on the extender before.

So with no wait, let’s get started…

To begin with, import AppCompat from AndroidX with this particular command –

“implementation ‘androidx.appcompat:appcompat:1.1.-alpha05’”

This AppCompat version will develop bug fixes and new updates plus the Day/Night theme. Alter the theme and extend exactly the same from Theme.AppCompat.DayNight.

Opt for some coding

Set the theme within Android Application Class and alter the present one while beginning your application. This process sets changes to current activities from v1.1.-alpha05. But you may also ensure that it stays safe in configuration changes without getting to recreate().

override fun onCreate()

Listed here are the modes used inside the application to select theme –

•           To set day/light theme – MODE_NIGHT_NO

•           To set night/dark theme – MODE_NIGHT_YES

•           To use system theme – MODE_NIGHT_FOLLOW_SYSTEM

•           To change to dark mode with battery saver enabled on device – MODE_NIGHT_AUTO_BATTERY

•           To set theme according to device time – MODE_NIGHT_AUTO_TIME

A RadioGroup can there be in a application to change between various styles –

private fun initThemeListener()

    private fun setTheme(themeMode: Int, prefsMode: Int)

Bear in mind that device should have Android 9 Cake to aid the machine theme. When the system is running a mature Android version, you have to take away the option to find the system theme in the UI.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) else

How to determine the current theme around the device?

There has to be a night or day theme to obtain the configuration but it’s difficult to determine if it’s set by hand, battery or even the system itself. By doing this, shared preferences are utilized to tick the best box and keep theme selected. Here’s the code –

when (getSavedTheme()) sources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK))

Once the user runs the application soon after installing it, it will likely be in undefined condition in shared preferences. By doing this, we ought to see if configuration includes a theme. Otherwise, the sunshine theme is going to be set as default.

Would you like additional control?

You may create a “values-night” folder to make use of custom colors for light/dark theme (for instance, light blue for light and fast for dark theme) and override colors file. Use attributes and override styles.xml if you wish to achieve this. The application uses colors only from “values-night” once the user chooses a dark theme. There’s also alternate sources for you to maintain night styles, for example, “drawable-night”.

Main Point Here

It isn’t so complicated to apply each dayOrevening theme with an application, especially when you’re while using AppCompat library. You may also convey more control on colors and styles by doing a bit of extra preparation. You want to do the look and implementation the best means by any project. Should you too did exactly the same on all of your projects , you can tell us.