nilkash nilkash - 1 year ago 57
Android Question

Change drawable start color and end color dynamically in android activity class

Hi I am developing one android application in which I drawable resource to set backgroung for button. I want to change start and end color for that drawable programatically i.e. in activity class inside button click listener. My drawable looks like :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=""
<gradient android:startColor="#be584c"
android:angle="270" />
<corners android:radius="2dp" />
<stroke android:width="1px"/>

And I set it as background for button in xml file.

and i want to change start color and end color of this drawable in activity class how to do this. Is there any way to f=do this. Need help. Thank you.

Answer Source

Yes, it is possible. You should use GradientDrawable to do this.

int colors[] = { 0xff255779, 0xffa6c0cd };

GradientDrawable gradientDrawable = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM, colors);


Change color code as per your requirement. Though I used Color.parseColor("color code"), its not working.

There are some option for Orientation like following.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download