Android: Center splash screen image

Slight change in code as it requires adding a second element for best fit

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2A7800"<---- optional background color to fit the image. to make it blend in
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"<--- centers
        android:adjustViewBounds="true"
        android:scaleType="fitXY"<--- makes sure the image fits into the layout my matching the screen size.

        android:src="@drawable/splash" />
</LinearLayout>

Adding an ImageView is best practice as it is better performance wise than using android:background and setting an image. I discovered this with a ScrollView and the master parent had android:background set to an image. It slowed down the ScrollView extremly.

Additionally, there is no need for a specific layout with this method. You can use RelativeLayout, FrameLayout, LinearLayout - anything that works. The exception might be ListView, GridView


You don't actually need neither LinearLayout nor RelativeLayout here, use a more lightweight FrameLayout instead.

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:src="@drawable/splash"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</FrameLayout>

I am using a background image for my activity. First create a theme in your styles.xml:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

Then create a new drawable with the name background_splash.xml

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

Then set your activity to use this theme in your AndroidManifest.xml:

<activity android:name="SplashActivity" android:theme="@style/SplashTheme" >

This worked for me.


You can use relative layout, try the below code..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher" />
</RelativeLayout>

Tags:

Android