'Force app widget's light theme on OnePlus phone with Android 12

I have a problem with forcing a light theme of home screen app widget on OnePlus 9 phone with Android 12 (using the OnePlus' launcher). My widget's theme inherits from Theme.MaterialComponents.Light and is light on Android 11 and below, even when the phone is in the dark mode.

Problem is on Android 12, where the widget's background is always very dark, when the phone is in the dark mode. When I set pure white background, it ends-up almost black. Pure red is dark red, pure green is dark green, etc. Looks like something in the system is tinting the background colors. The same is happening with Theme.Material3.Light and @android:style/Theme.DeviceDefault themes.

Everything is good on pure Android in an emulator, so this looks like a OnePlus issue. Any ideas what can be wrong and how to fix this?



Solution 1:[1]

Goto to your themes file and place the following line in the style you are using for your app.

<item name="android:forceDarkAllowed">false</item>

Solution 2:[2]

You can create a Application Class which extends Application() and place the following code in OnCreate() like this

    class YourApplicationClass : Application(){
    override fun onCreate() {
        super.onCreate()
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
    }
}

Then in your manifest file add this application class like this

<application
        android:name=".base.YourApplicationClass" 
        ...
       />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Syed Faizan Ali
Solution 2 Mohsin Ali