Jawad AlZaabi
Jawad AlZaabi

Reputation: 75

Android Gradient Background drawable does not work

I've created this drawable resource, backgroundactivity, to use as my app's background:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<item
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <shape
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <gradient
            android:angle="90"
            android:startColor="#BFEFFF"
            android:centerColor="#B0E2FF"
            android:endColor="#82CFFD"
            android:type="linear"/>
    </shape>
</item>
</selector>

I want to set this background on all my activity, so I've tried to put this drawable resource in the styles.xml like this:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:background">@drawable/backgroundactivity</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="@android:background">@color/colorPrimary</item>
</style>
</resources>

But in this way it didn't work: the background still stay white as default. I've also tried to replace android:background with android:windowBackground, but it causes my application to be completely black.

Thanks in advance!

Upvotes: 0

Views: 3044

Answers (2)

Jawad AlZaabi
Jawad AlZaabi

Reputation: 75

It worked by simply editing the background activity.xml to the following:

<?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape>
        <gradient
            android:angle="90"
            android:startColor="#BFEFFF"
            android:centerColor="#B0E2FF"                
            android:type="linear" />
    </shape>
</item>
</selector>

Upvotes: 0

Prithvi Bhola
Prithvi Bhola

Reputation: 3151

Try to use windowBackground instead of background

<item name="@android:windowBackground">@color/colorPrimary</item>

Upvotes: 1

Related Questions