title: Cordova Android set windowBackground programmatically at runtime
body: <p>When the Cordova android app launches, <strong>a blank screen is briefly visible</strong> before cordova-plugin-splashscreen kicks in. I have learned that this is the <strong>windowBackground colour</strong> and can be changed by making a custom styles.xml and referencing it within AndroidManifest.xml though the activity's android:theme property. Example:</p>
  
  <p>From AndroidManifest.xml:</p>
  
  <pre><code>&lt;activity android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|screenSize" android:label="@string/activity_name" android:launchMode="singleTask" android:name="MainActivity" android:screenOrientation="portrait" android:theme="@style/CustomStyle" android:windowSoftInputMode="adjustPan"&gt;
  </code></pre>
  
  <p>From styles.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;style name="CustomStyle" parent="@android:style/Theme.Material.Light.NoActionBar"&gt;
          &lt;item name="android:windowBackground"&gt;@drawable/init_splash&lt;/item&gt;
      &lt;/style&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>styles.xml references another file just containing a drawable colour.</p>
  
  <p>This works. It allows me to change the colour that appears before the splash screen.</p>
  
  <p>However, I am now looking to allow the user to <strong>optionally change to a dark theme</strong>. I have already figured out how to modify cordova-plugin-splashscreen to change the splashscreen based on the user preference, but I'm having trouble <strong>changing the windowBackground/theme programatically at runtime</strong>.</p>
  
  <p>I have tried adding the following within MainActivity.java or CordovaActivity.java:</p>
  
  <pre><code>setTheme(R.style.CustomDarkStyle);
  
  getWindow().setBackgroundDrawableResource(getResources().getIdentifier("init_splash_dark", "drawable", getPackageName()));
  
  getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK));
  </code></pre>
  
  <p>I placed these in the onCreate before super.onCreate() or setContentView(). The window background colour does indeed change, <strong>but the initial blank screen before the splash stays at whatever colour was set in the manifest</strong>.</p>
  
  <p><strong>How can I change the activity/window background colour programmatically when the application is started?</strong></p>
  
  <p><a href="https://stackoverflow.com/a/48273920/6276471">Some have suggested</a> changing the app theme to a transparent one to prevent the blank screen entirely, but that causes a delay in opening the app. I'm fine with the blank screen, I just want to change it's colour programmatically.</p>
  
  <p>As of April 22nd, I am yet to find a solution to this problem.</p>