windowBackground not picking night color in styles when dark theme enabled

796 views Asked by At

i have implemented dark ui in my android app everything is working fine but i have a launcher activity which has windowBackground in styles like this

   <style name="AppTheme.Launcher" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="android:windowBackground">@drawable/l_launch_screen</item>
    </style>

and the l_launch_screen is this

<?xml version="1.0" encoding="utf-8"?>

<!-- The android:opacity=”opaque” line — this is critical in preventing a flash of black as your theme transitions. -->
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">

    <!-- The background color, preferably the same as your normal theme -->
    <item android:drawable="@color/colorPrimaryDark"/>

    <!-- Your product logo - 144dp color version of your app icon -->
    <item>
        <bitmap
            android:src="@drawable/app_round_icon"
            android:gravity="center"/>
    </item>

</layer-list>

and in this colorPrimaryDark has two color one is night and other is simple now the issue is that when the dark theme is activated by android by selecting Dark theme in android q notification panel android:windowBackground is picking night color but when Dark theme is disabled by android and dark theme is selected in my app by setting this in application class

   AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

android:windowBackground is not picking the night color but all other activities is picking the night color perfectly I have seen the same behavior with WhatsApp app so is this a bug or i am doing something wrong I have also tried define different styles for the night and for drawable but happening the same issue

1

There are 1 answers

0
rhiskey On

This is how to implement loading screen using Activities approach.

  1. Add 2 new resource files:
  • for light theme splash_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/white" />
    <item>
        <bitmap android:gravity="center"
            android:src="@drawable/logo"/>
    </item>
</layer-list>
  • for dark theme splash_background.xml (same name) place it in drawable-night folder (create if you dont have it ) and you have to add in colors.xml custom dark color (for example <color name="black_700">#191414</color>) :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/your_dark_background_color" />
    <item>
        <bitmap android:gravity="center"
            android:src="@drawable/logo"/>
    </item>
</layer-list>
  1. Finally In your themes.xml:
    <style name="Theme.AppName.SplashTheme" parent="Theme.AppName">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>
  1. Good approach to create splashscreen as theme of existing style and show it when app started: to do that create new Activity
class SplashScreenActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        setTheme(R.style.Theme_AppName_SplashTheme)
        super.onCreate(savedInstanceState)

        startActivity(Intent(this,MainActivity::class.java))
        overridePendingTransition(0, 0) //this one to remove animation
        finish()
    }
}
  1. Apply this theme on your SplashScreenActivity in AndroidManifest.xml:
        <activity
            android:name=".SplashScreenActivity"
            android:excludeFromRecents="true"
            android:exported="true"
            android:launchMode="singleInstance"
            android:theme="@style/Theme.AppName.SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.MULTIWINDOW_LAUNCHER" />
            </intent-filter>
        </activity> 
  1. In your MainActivity.kt you have to add Intent.FLAG_ACTIVITY_NO_ANIMATION to remove annoying swipe animation when switching activity:
    companion object {
        fun startFrom(context: Context) {
            val intent = Intent(context, MainActivity::class.java)
            intent.flags =
                Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NO_ANIMATION 
            context.startActivity(intent)
        }
    }