When developing an Android Application, you may want to control the appearance of your application by forcing a specific theme, either Light or Dark regardless of the user's device settings. This is mandatory for maintaining a smooth user experience or aligning your application's branding. You may want the application to be in Light Mode only or Dark Mode only. However, if the user's device settings are opposite, then it may cause UI issues.
Why Forcing a Theme is Important
1. System Theme Conflicts
Suppose your app is designed for Light Mode, and you've set backgrounds to white or other light colors. If the user's device is in Dark Mode, Android might apply light-colored text and UI elements on top of your light background resulting in unreadable content. While you can manually specify text and background colors for each element, this quickly becomes tedious and error-prone, especially across many screens and components.
2. Status Bar Visibility Issues
Another common issue is with the status bar, which displays time, battery, and notifications. In Dark Mode, the system often sets the status bar text to a light color. If your app’s background is also light (because you intended it for Light Mode), the status bar content becomes nearly invisible. This leads to a poor user experience and a visually broken UI. The same issue applies in reverse when your app is designed for Dark Mode, but the system is in Light Mode.
Here's a example of the above problems faced by developers. You can see the status bar is nearly invisible color.

It's really frustrating to deal with every component like this and have invisible status bar as it looks unappealing. Fortunately, there's a simple, one-line fix using AppCompatDelegate. By enforcing your preferred mode in a custom Application class, you ensure your app theme stays consistent, regardless of the system settings.
Step by Step Implementation
Step 1: Creating an Application class
The Application class in Android maintains global app state. It’s the first class initialized when your app launches, making it the perfect place to set your preferred UI mode.
To know more about Application Class, refer to the following link: Click Here.
Navigate to app > java+kotlin > {package-name} and create a new Kotlin Class and name it anything for eg. MyApp.kt. Inherit the Application Class and override its onCreate function. Write the code to force the application in preferred mode. It is a very simple one line code.
For Forcing Light Mode:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)For Forcing Dark Mode:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)MyApp.kt:
package org.geeksforgeeks.demo
import android.app.Application
import androidx.appcompat.app.AppCompatDelegate
class MyApp: Application() {
override fun onCreate() {
super.onCreate()
// For forcing Light Mode always
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
// For forcing Dark Mode always
// AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
}
Step 2: Adding the Application class in manifest
You need to register your custom Application class in the AndroidManifest.xml. If you don't, your custom class won't be executed and the application won't be forced to run in your preferred mode.
Navigate to app > manifest > AndroidManifest.xml and add a name attribute inside the application tag, with your custom application class name as value.
android:name=".YOUR-CLASS-NAME"for eg.
<application
android:name=".MyApp"
...
</application>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:name=".MyApp"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Demo"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Now your Application will always run in your preferred mode regardless of Device Settings
Output:
