Adding a dark / night mode theme to your app can be a great option for your users – it saves battery life, and is much easier on the eyes at night. A white background on black text, or any combination of ‘bright’ colors in your UI, is really stressful on the eyes, particularly at night. Thus, a dark background with lighter text is much less stressful and harmful to the eyes, which is why many popular apps have started including a dark theme – YouTube and Reddit most recently, although Facebook still hasn’t published theirs after promising one back in May. If you’d like to easily add a toggleable dark mode to your Android app, follow this Appuals guide and comment if you run into any issues!
Creating the XML Attributes
First you need to create an XML attributes file that will handle the theming. This is much better than including two sets of resources in your APK for two different themes, and thus bloating your APK size, as Android can natively change colors on attributes through this XML. So in your app’s ‘resources’ folder, create a file named attrs.xml and add these lines (these are styleable attributes): Prior to Lollipop we could not specify attributes for drawables, so we had to specify the resource IDs for drawables – however, this is no longer the case.
Adding the Styles
To make this work, we need to create two separate styles which will share the same base. The first will obviously be your primary ‘light’ theme, and the second will be your ‘dark’ theme. Here are the actual styles for a given activity: You need to specify one of these themes into the Manifest for the app’s activity, which in this case is FeedActivity.
Stylizing the Views
Here’s an example of stylizing your views – in this example, its for a “Page not found” redirect error.
How to Enable Dynamic Theme Switching
The best and most efficient way to enable dynamic theme switching is to load the SharedPreference valued which is used to store the setting into the Application instance using the Singleton pattern, which means we won’t have to do it at the start of each Activity. Because this instance is being launched before anything else, it allows you to call isNightModeEnabled() Boolean whenever you want, in any activity belonging to the app once it has been opened. However, if you want to apply a different theme than the one that is specified in the manifest file, it must be done before calling the parent onCreate() method.
Setting The State Using AppCompat
AppCompat is really useful for a wide range of things, and in this case, we’ll be using it to set the mode state. This means that you no longer need to use the app instance to create a cached state, thus cutting down on app cache bloat (albeit by a very tiny margin). Here, you can either use:
AppCompatDelegate.MODE_NIGHT_YES AppCompatDelegate.MODE_NIGHT_NO AppCompatDelegate.MODE_NIGHT_AUTO
Retrieving The State
That should be it – you can continue customizing and tweaking values to your liking, but this guide is a great introduction to get you started with including a native dark theme in your Android app.
Fix: File Explorer Dark Theme Not Working on Windows 10How to Enable Dark Theme on Windows 10Super Mario RTX Features A Dark and Gritty Theme Powered by Unreal Engine 5How to Install Google Docs Dark Theme