Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Splash Screen shifts

I have an Android Splash Screen which renders a drawable. When it is opened via a cold start, I find that my asset simply shifts in an upward direction.

You can find the appropriate code below, all unnecessary code has been omitted.

Here's the slight shift:

enter image description here

SplashActivity.java

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    SplashScreen.show(this, R.style.SplashTheme);
    super.onCreate(savedInstanceState);
}

res/drawable/background_splash.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
    <item android:drawable="@color/splash_background_color"/>
    <item
        android:gravity="center"
        >
        <bitmap
            android:src="@drawable/x150"/>
    </item>
</layer-list>

res/layout/launch_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/splash_background_color">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/background_splash"
        />
</FrameLayout>

res/values/styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>
    <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/background_splash</item>
    </style>
</resources>
like image 590
Dan Avatar asked Feb 18 '19 16:02

Dan


2 Answers

In styles.xml, replace:

<item name="android:windowBackground">@drawable/background_splash</item>

with

<item name="android:background">@drawable/background_splash</item>

Note the windowBackground -> background

That solves the issue for me.

like image 96
lateus Avatar answered Oct 17 '22 12:10

lateus


I found the solution:

You should remove ImageView because you've already set splash via android:windowBackground. Also remove android:background="@color/splash_background_color" from FrameLayout to make it transparent

Btw, you could delete res/layout/launch_screen.xml if you are not going to draw some layouts over your splash.

For Activity don't call setContentView()

For Fragment don't override onCreateView()

It's ok, Android doesn't require to set layout for them.

like image 7
Taras Parshenko Avatar answered Oct 17 '22 12:10

Taras Parshenko