Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Gradient Background programmatically

I have an image on which I'm putting a colored overlay, like this (the colors are taken from here):

layout/list_item_view.xml

<View 
    android:id="@+id/image_cover_gradient"
    android:layout_width="fill_parent"
    android:layout_height="80dip"
    android:layout_alignParentTop="true"
    android:layout_marginTop="70dp"
    android:background="@drawable/gradient_blue"        
    />

drawable/gradient_blue.xml

<?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="@color/CornflowerBlue"
            android:endColor="@color/Transparent"
            android:type="linear" />
    </shape>
</item>
</selector>

This always puts a blue overlay (CornflowerBlue) and it works as expected.

Now I'm trying to do this programatically and followed some stackoverflow answers (such as this), but still can't make it work. Here's my code:

private void setColor(int color){
    View gradientCover = view.findViewById(R.id.image_cover_gradient);
    // this FAILS because it's a StateListDrawable
    //GradientDrawable coverGd = (GradientDrawable) gradientCover.getBackground();
    //coverGd.setColor(color);

    //this doesn't seem to work either (I don't see any effect on the image)
    GradientDrawable drawable = new GradientDrawable(
            Orientation.BOTTOM_TOP, new int[] { color, resources.getColor(R.color.Transparent) 
            });
    StateListDrawable sld = new StateListDrawable();
    sld.addState(new int[] { android.R.attr.startColor, android.R.attr.endColor}, drawable);
    gradientCover.setBackground(sld);
}
like image 749
Haji Avatar asked Sep 01 '14 14:09

Haji


People also ask

How to set gradient color background for a button in Android?

Gradient color background for a Button in Android through programming. Application developer can set gradient effect through MainActivity.java programming file on another button click event or directly shows on activity start time.

How to add gradient background to activity_main?

It is only used with the radial gradient. Now open the activity_main.xml file and remove the default code and change the layout to RelativeLayout and set it's background to gradient background as shown below: with this our activity_main.xml is done and the complete code will look like:

What is a gradient background?

We have seen the Gradient colors on many websites as backgrounds, used in App logos, like in the Instagram logo shown below, App background, buttons, progress bars, etc. A gradient makes the UI of any app, be it Mobile App or a website, more beautiful and vibrant.

How to create a gradient drawable in Android?

So go to app -> res -> drawable and right-click on drawable -> New -> Drawable Resource File and create gradient_drawable.xml file. As you can see in the code above, we are using the gradient tag along with providing android:startColor, android:centerColor and android:endColor attributes to define the color that will be used in the gradient.


1 Answers

As @pskink suggested - removing the StateListDrawable solved it:

GradientDrawable drawable = new GradientDrawable(
    Orientation.BOTTOM_TOP, new int[] { color, resources.getColor(R.color.Transparent) 
});     
gradientCover.setBackground(drawable);
like image 137
Haji Avatar answered Oct 30 '22 14:10

Haji