shanwu shanwu - 1 year ago 149
Android Question

how to get the color from GradientDrawable

First, I set the green color to be the background of the View mIcon,

View mIcon = findViewById(;

GradientDrawable gdraw = (GradientDrawable) mContext.getResources().getDrawable(R.drawable.roundbtn_white_normal);

Then, I don't know how to get the color from this View's background... there's no getColor()

Answer Source

the following class works fine for me so far.

import android.content.res.Resources;

// it's the same with the GradientDrawable, just make some proper modification to make it compilable
public class ColorGradientDrawable extends Drawable {
    private int mColor; // this is the color which you try to get
    // original setColor function with little modification
    public void setColor(int argb) {
        mColor = argb;

    // that's how I get the color from this drawable class
    public int getColor() {
        return mColor;

    // it's the same with GradientState, just make some proper modification to make it compilable
    final public static class GradientState extends ConstantState {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download