Skip to content Skip to sidebar Skip to footer

Android: Setting Window Background On Launched Activity

So I've read Romain Guy's blog post on setting the Window background and percieved performance, and am trying to emulate that. It is such a simple solution and not sure why I can'

Solution 1:

Try setting window background before launching the new activity programmatically.

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

Solution 2:

I have ran into the same problem and wasted my day on it. We can't set the theme in java as in my case the control came to onCreate of my splash activity very late. Till that time black screen keep visible.

This gave me clue that window has to be managed from theme which we specify in manifest.

There is the manifest i have:

<activityandroid:name=".main.activities.SplashScreen"android:theme="@style/Splash"android:screenOrientation="portrait"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity>

Now The theme i created is as follows:

<stylename="Splash"parent="@style/Theme.AppCompat.Light"><itemname="android:windowBackground">@drawable/splash</item><itemname="android:windowNoTitle">true</item><itemname="windowNoTitle">true</item><itemname="colorPrimaryDark">@color/green_09</item><itemname="colorPrimary">@color/green_09</item><itemname="windowActionBar">false</item></style>

Splash in the drawable resource which contains a bitmap resource, I have to tweak a bit to make it look perfect not stretched and in center:

<bitmapxmlns:android="http://schemas.android.com/apk/res/android"android:antialias="true"android:dither="true"android:gravity="fill"android:src="@drawable/splash_screen" />

Solution 3:

it works for me if i declared this in a Theme Style in styles.xml :

<?xml version="1.0" encoding="utf-8"?><resourcesxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><stylename="CustomTheme"parent="android:Theme.Holo.NoActionBar"><itemname="android:windowBackground">@color/bg_evening</item></style></resources>

and in the manifest i set this theme to my application :

<applicationandroid:name="com.my_app.pack"android:theme="@style/CustomTheme" >
...

or to one of my activities :

<activityandroid:name="..."android:theme="@style/CustomTheme">
...

you can declare muliple theme styles and add any one of them to any of your activities, which will override the theme set to the application

Post a Comment for "Android: Setting Window Background On Launched Activity"