Search code examples
androidbackgroundwindowdrawablegradient

Set radial gradient as window background


I have the following gradient_bg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:type="radial"
        android:gradientRadius="200"
        android:startColor="#666666"
        android:endColor="#AAAAAA"
        />
</shape>

I would like to use this gradient as the android:windowBackground setting for my application. The application would then have a few views with transparent backgrounds on top of it.

I've tried applying this gradient directly using:

android:windowBackground="@drawable/gradient_bg"
within my application manifest, and then setting the scrollview background as transparent, but I just get the default black background.

How would I go about achieving this result? I specifically don't want to apply the background to the scrollview.

Thanks for your help in advance.


Solution

  • Another way might be to create a theme (in values/styles.xml):

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="windowbg" parent="android:Theme">
            <item name="android:windowBackground">@drawable/rectangle</item>
        </style>
    </resources>
    

    Now you can add this theme to your activity/application like this:

    android:theme="@style/windowbg"