SplashScreen with Vector stretched full screen

AndroidAndroid LayoutSplash ScreenAndroid Vectordrawable

Android Problem Overview


I did my splash screen with this tutorial and it works great:

Basically I set up a splascreen through theme:

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

I wanted to put a vector image inside like this: (drawable_splashcreen)

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

The image will streched through the full screen. On API 23 it works like it should have. But on older devices it just streches. I tried width, height and even messed up with viewports but no success.

Android Solutions


Solution 1 - Android

I stumbled upon the same problem. Unfortunately there does not seem to be a possibility to make the splash screen work with just a vector drawable for pre API 23.

The problem is you can't load VectorDrawableCompat outside of the process, like in this case in your themes android:windowBackground. So what is likely happening here is, that on API 21 the Vector get's converted to a PNG to be compatible. So in the <layered-list>the converted PNG is inserted into the <item> element, which causes the bitmap to stretch to all edges, because it's missing the <bitmap> element.

So my solution is the following: Create a drawable_splashscreen.xml inside the folder drawables-v23 which looks like the following for the vector drawable.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:opacity="opaque">
    <item android:drawable="?attr/colorPrimary"/>
    <item android:drawable="@drawable/ic_splashscreen" android:gravity="center"/>
</layer-list>

Then create another drawable_splashscreen.xml but inside the regular drawables folder:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:opacity="opaque">
    <item android:drawable="?attr/colorPrimary"/>
    <item>
        <bitmap android:src="@drawable/ic_splashscreen" android:gravity="center"/>
    </item>
</layer-list>

Notice the <bitmap> element. So now, when the PNG is used on pre API 23 devices it will be displayed properly and won't be stretched to the whole background.

Unfortunately you also have to provide splash screen as PNG for this to work in the old APIs.

But for every device with API 23+ the vector drawable will be used.

Solution 2 - Android

For full screen splash try to use:

android:gravity="fill_horizontal|fill_vertical"

> If not resolve maybe the solution is create separated images > for each resolution size.


Most common resolutions:

  • Small = 240 x 320px (ldpi)
  • Medium = 320 x 480px (mdpi)
  • Large = 480 x 800px (hdpi)
  • xLarge = 640 x 960px (xhdpi)

Portrait Format:

  • ldpi = 240 x 360px (0.75 x mdpi)
  • mdpi = 320 x 480px (base density)
  • hdpi = 480 x 720px (1.5 x mdpi)
  • xhdpi = 640 x 960px (2 x mdpi)
  • xxhdpi = 960 x 1440px (3 x mdpi)
  • xxxhdpi = 1080 x 1920px (4 x mdpi)

Landscape Format (inverted portrait format):

  • ldpi = 360 x 240px (0.75 x mdpi)
  • mdpi = 480 x 320px (base density)
  • hdpi = 720 x 480px (1.5 x mdpi)
  • xhdpi = 960 x 640px (2 x mdpi)
  • xxhdpi = 1440 x 960px (3 x mdpi)
  • xxxhdpi = 1920 x 1080px (4 x mdpi)

More about you can find here:

  1. https://design.google.com/devices/

  2. https://stackoverflow.com/questions/10574363/android-splash-screen-image-sizes-to-fit-all-devices

  3. http://vinsol.com/blog/2014/11/20/tips-for-designers-from-a-developer/

Solution 3 - Android

Edited solution that will make your SplashScreen look great on all APIs including API21 to API23

First of all read this article and follow the GOOD way of making a splash screen.

If your logo is distorted or wont fit and you are only targeting APIs24+ you can simply scale down your vector drawable directly in its xml file like so:

<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:aapt="http://schemas.android.com/aapt"
android:viewportWidth="640"
android:viewportHeight="640"
android:width="240dp"
android:height="240dp">
<path
    android:pathData="M320.96 55.9L477.14 345L161.67 345L320.96 55.9Z"
    android:strokeColor="#292929"
    android:strokeWidth="24" />
</vector>

in the code above I am rescaling a drawable I drew on a 640x640 canvas to be 240x240. then i just put it in my splash screen drawable like so and it works great:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque"
android:paddingBottom="20dp" android:paddingRight="20dp" android:paddingLeft="20dp" android:paddingTop="20dp">

<!-- The background color, preferably the same as your normal theme -->
<item>
    <shape>
        <size android:height="120dp" android:width="120dp"/>
        <solid android:color="@android:color/white"/>
    </shape>
</item>

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

</item>
</layer-list>

my code is actually only drawing the triangle in the picture at the bottom but here you see what you can achieve with this. Resolution is finally great as opposed to the pixelated edges I was getting when using bitmap. so use a vector drawable by all means (there is a site called vectr that I used to create mine without the hasle of downloading specialized software).

EDIT in order to make it work also on API21-22-23

While the solution above works for devices runing API24+ I got really disappointed after installing my app a device running API22. I noticed that the splashscreen was again trying to fill the entire view and looking like shit. After tearing my eyebrows out for half a day I finally brute-forced a solution by sheer willpower.

you need to create a second file named exactly like the splashscreen xml (lets say splash_screen.xml) and place it into 2 folders called drawable-v22 and drawable-v21 that you will create in the res/ folder (in order to see them you have to change your project view from Android to Project). This serves to tell your phone to redirect to files placed in those folders whenever the relevant device runs an API corresponding to the -vXX suffix in the drawable folder, see this link. place the following code in the Layer-list of the splash_screen.xml file that you create in these folders:

<item>
<shape>
    <size android:height="120dp" android:width="120dp"/>
    <solid android:color="@android:color/white"/>
</shape>
</item>

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

</item>

these is how the folders look

For some reason for these APIs you have to wrap your drawable in a bitmap in order to make it work and jet the final result looks the same. The issue is that you have to use the aproach with the aditional drawable folders as the second version of the splash_screen.xml file will lead to your splash screen not being shown at all on devices running APIs higher than 23. You might also have to place the first version of the splash_screen.xml into drawable-v24 as android defaults to the closest drawable-vXX folder it can find for resources.

my splashscreen

Solution 4 - Android

I do not think this is possible to do with vectors, for devices < API 23, since it is not possible to set the attributes android:height and android:width on the drawable.

To implement my splash screen with a centered icon, I had to export the vector of my logo to .png's for each screen size, and embed a bitmap within the layer-list:

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

    <item
        android:left="20dp"
        android:right="20dp">
        <bitmap
            android:gravity="center"
            android:scaleType="centerInside"
            android:src="@drawable/logo_rasterised" />
    </item>

</layer-list>

Ideally I would not like to have any bitmap images at all in my resources, but at least the splash screen is the only place where I have had to use bitmaps.

Solution 5 - Android

Use bitmap and define you image in the src. Set gravity to center

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

    <item android:drawable="@color/color_background_splash_screen"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/ic_logo_splash"/>
    </item>
</layer-list>

splash screen

Solution 6 - Android

The simplest method I found to solve this is by using a vector asset. This will ensure that the apk size is not big too. If you're not doing this or don't have an svg file for your logo, you should manually change the DP intensities of pictures and add them manually. You may also use plugins that do that automatically for you.

In case you go with the vector file and still end up getting a zoomed image, add in android:top, left right and bottom to get your logo size right.

Solution 7 - Android

Dont put your image in window:Background, instead add as android:src for an imageview for the layout of the SplashScreenActivity.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWinterChillyView Question on Stackoverflow
Solution 1 - AndroidAirbagmanView Answer on Stackoverflow
Solution 2 - AndroidℛɑƒæĿᴿᴹᴿView Answer on Stackoverflow
Solution 3 - AndroidquealegriamasalegreView Answer on Stackoverflow
Solution 4 - AndroidKirkView Answer on Stackoverflow
Solution 5 - AndroidAvinash kumawatView Answer on Stackoverflow
Solution 6 - AndroidHarshaView Answer on Stackoverflow
Solution 7 - AndroidsaDashiv sinhaView Answer on Stackoverflow