Splash screen activity background color
Asked Answered
C

5

23

I have problem with my splash screen on Android. Splash screen is displayed to the user during long application startup but activity background is always black. I mean background bitmap (splash image) is visible, but background is black instead of white. I'm using PNG image with transparency.

What I have:

  1. PNG splash screen image with transparent background
  2. Splash screen activity
    [Activity(MainLauncher = true, Theme = "@style/Theme.Splash", NoHistory = true)]
    public class SplashScreen : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Do your app initialization here
            // Other long running stuff

            // Run app when done
            StartActivity(typeof(MainForm));
        }
    }
  1. Theme style for splash screen activity in resources/values/styles.xml
    <resources>
      <style name="Theme.Splash" parent="@android:style/Theme.Holo.Light">
        <item name="android:windowBackground">@drawable/splash_centered</item>
        <item name="android:windowNoTitle">true</item>
      </style>
    </resources>
  1. Splash drawable in resources/drawable/splash_centered.xml
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/splash"
        android:gravity="center"
        android:background="@color/white"> <!-- this is ignored -->

Problem: As you can see, I'm using Theme.Holo.Light as parent theme and I'm using it in the rest of my app. Holo light is using white background. This white background is not applied on SplashActivity background. SplashActivity background is always black. Background bitmap (splash image) is visible, but background is black instead of white. I'm using PNG image with transparency.

Question: How to set default Holo.Light theme background color (white) on the SplashScreen activity?

Note: I'm using Xamarin.Android, but styling is common for Android platform. Android version 4 and above.

Corder answered 2/10, 2014 at 5:42 Comment(4)
do you set content view in the splash view?Victualer
No. It is done with styles only. There is no layout.Corder
@Ludwo, did you make it work?Divalent
No, background is still blackCorder
N
53

In resources/drawable/splash_centered.xml, instead of the bitmap use a layer-list

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="rectangle">
      <solid android:color="@android:color/white" />
    </shape>
  </item>
  <item>
    <bitmap android:gravity="center" android:src="@drawable/splash" />
  </item>
</layer-list>
Navicular answered 20/3, 2015 at 10:43 Comment(4)
works great, actually better than solution with custom activity as theme displays image earlierAsparagus
Where does this go?Tatman
@IanWarburton in resources/drawable/splash_centered.xml. Original question step 4, layer-list instead of the bitmap.Magnetoelectricity
That worked perfectly... I wonder if anyone can share some path related to learning UI stuff like layer-list,vector images, etc.. there is so much that it gets confusingTrometer
M
12

This is how I was able to get white background splash (logo centred) in Xamarin.

[Activity (Theme= "@style/Theme.Splash", MainLauncher=true, NoHistory=true)]            
public class SplashActivity : Activity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.splash);
        ThreadPool.QueueUserWorkItem (o => LoadActivity ());
        // Create your application here
    }
    private void LoadActivity() {
        Thread.Sleep (1000); // Simulate a long pause
        RunOnUiThread (() => StartActivity (typeof(MainActivity)));
    }
}

with Theme.Splash as:

<resources>
  <style name="Theme.Splash" parent="@android:style/Theme.Light">
    <item name="android:colorBackground">@android:color/white</item>
    <item name="android:windowNoTitle">true</item>
  </style>
</resources>

and splash.axml code (Theme.Light.NoTitleBar) as:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    android:gravity="center">
    <ImageView
        android:src="@drawable/splash"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView1"
        android:layout_gravity="center" />
</LinearLayout>

NB: There is a slight delay in the splash png (logo) to come up, but its still acceptable, better than the black background.

Mothering answered 28/11, 2014 at 18:19 Comment(4)
My background bitmap (splash image) is visible, but background colour is black instead of white. That's the point. I'm using PNG image with transparency. I updated my question to be more clear.Corder
By the above workaround you would get a white background to your transparent splash image (in SplashActivity).Mothering
The only solution I've found online that worked. Thanks.Lineman
It does work, but it causes quite a stutter when the next activity starts. I suggest just loading a white background and then in the main activity you can show a logo while you are creating your app. This is the case for me because our app takes between 5 and 10 seconds to load, and we were already showing a logo.Taker
O
1

set android:drawable="@color/colorWhite" to item.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/colorWhite" />

    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/splash" />
    </item>
</layer-list>
Obituary answered 17/7, 2018 at 6:14 Comment(0)
L
0

For the record, I used ionic/capacitor extension in VSC. Then the code generated has a theme. I had to change the @drawable/splash by @android:color/white, otherwise the background was always greyish.

<?xml version="1.0" encoding="utf-8"?>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    
    
</style>

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:background">@null</item>
</style>


<style name="AppTheme.NoActionBarLaunch" parent="Theme.SplashScreen">
    <!-- <item name="android:background">@drawable/splash</item> -->
    <item name="android:colorBackground">@android:color/white</item>
</style>
Licentiate answered 18/1 at 11:25 Comment(0)
B
0

Simple..! Add this item to your Theme style:

    <item name="android:windowSplashScreenBackground">#FEE9F2</item>

Boom..! your Splash screen would have that color as your background..!

Biernat answered 2/8 at 0:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.