26

How do I use a background image in an Activity theme/style?

If I do this using a colour:

<style name="CustomTheme" parent="android:Theme.Light">
    <item name="android:windowBackground">@color/custom_theme_color</item>
</style>

It works correctly, but if I replace the line with:

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

The image displays correctly, but all the content is squished into a tiny block in the top left corner of the screen. The shadow underneath the status bar is also cut off or messed.

I am using Mono for Android and the image is a valid nine-patch png.

Matthew
  • 3,951
  • 2
  • 21
  • 43
  • 2
    For everybody reading this here...be aware that your drawable which you use in windowBackground as a customized AppTheme will be loaded fully decoded into your heap. Even a small image in size (like 90KiB) can result in an allocation of 90MiB and more, because of the decoding done in runtime. This will cause OutOfMemory issues faster than light. :) – JacksOnF1re Mar 28 '17 at 14:37
  • For additional information, read this article how to setup splash screen correctly: https://www.bignerdranch.com/blog/splash-screens-the-right-way/ – JacksOnF1re Mar 28 '17 at 15:53

5 Answers5

16

I also don't use themes, so in the layout of the activity I would add

android:background="@drawable/splash_image"
VicVu
  • 6,655
  • 5
  • 50
  • 89
  • 4
    Thanks guys, but I need to use a theme as the layout content comes to late. In mono for android, there is a few seconds delay in the start up. I need to have the image displayed before the runtime is actually started. As the layout is only applied after the runtime has started, I have to use the theme, as it is applied immediately. – Matthew Jan 19 '12 at 20:41
  • No problem. I'm looking into themes right now because of a question I asked so if I figure it out through that I'll get back to you. – VicVu Jan 19 '12 at 20:49
  • The problem with the code is that, if we have a "EditText" control, and when keyboard appears the background image is squeezing. – Satyam Jul 17 '15 at 03:41
10

My solution was change the android:windowBackground to android:background.

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
  <style name="Theme.Splash" parent="android:Theme">
    <item name="android:background">@drawable/splash</item>
    <item name="android:windowNoTitle">true</item>
  </style>
</resources>
VeYroN
  • 670
  • 8
  • 17
8

I don't use themes or anything, so I'm not sure how that will affect this, but I set my background like this:

getWindow().setBackgroundDrawableResource(R.drawable.background_image);

in onCreate

bschultz
  • 4,166
  • 1
  • 22
  • 33
  • 8
    I discourage this approach, because it's cleaner to define it in the theme. It allows Android to display it immediately when the activity is launched, instead of seeing a short blink as the code is executed. – Paul Lammertsma Jan 12 '16 at 22:29
  • @PaulLammertsma I don't think you will see any blink as onCreate is executed before the view is even shown, at `onCreateView` – Luiz Jan 17 '19 at 12:32
5

I needed to have a splash image that looked like my app's initial activity and ran into same issue. I had good luck with windowContentOverlay instead of windowBackground. The drawable appeared below the status bar in the exact same position as a real layout would. It has worked on Android 2.2, Android 3.0, and Android 4.1.

This is my style:

<style name="SplashTheme" parent="android:style/Theme.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@drawable/splash</item>    
</style>

My splash.xml drawable mimics my user interface header using layer-list:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--- Background; tile is broken on Android 2.2 so just have super wide background that will get clipped  -->
    <item>
        <bitmap android:gravity="top|left" android:src="@drawable/splash_background" />
    </item>

    <!--- Icon left justified -->
    <item>
        <bitmap android:gravity="top|left" android:src="@drawable/header_icon" />
    </item>

    <!--- Buttons/etc right justified  -->
    <item>
        <bitmap android:gravity="top|right" android:src="@drawable/splash_buttons"  />
    </item>
</layer-list>

I believe ActionBar also has some built-in ways to handle this, if your app uses it. The MonoIO sample seems to have such a launch image.

t9mike
  • 1,434
  • 1
  • 18
  • 30
  • Is it possible to add a textview to your splash.xml ? I need to display a splash screen with the logo and a text such as Connecting... – v4r Jun 04 '14 at 13:40
  • 1
    how to resize the background image, so it fits the width of the screen and keeps the aspect ratio, with this solution? – pfaehlfd Nov 11 '17 at 11:09
  • I have not done Android development in years. Recommend new StackOverflow question. – t9mike Nov 12 '17 at 12:07
0

You can use the android:gravity="fill" instead of android:gravity="center",

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

    <item>
        <bitmap
            android:gravity="fill"
            android:src="@drawable/splash_screen" />
    </item>
</layer-list>

Note: It will depend on your shape and size of the splash image. In my case by the use of android:gravity="fill" my splash is looking perfect. You can use the different attribute of gravity on the type of your splash image.

Aman Gupta - ΔMΔN
  • 2,999
  • 1
  • 17
  • 37