Example usage for android.graphics.drawable StateListDrawable getConstantState

List of usage examples for android.graphics.drawable StateListDrawable getConstantState

Introduction

In this page you can find the example usage for android.graphics.drawable StateListDrawable getConstantState.

Prototype

@Override
    public ConstantState getConstantState() 

Source Link

Usage

From source file:Main.java

public static void configureColor(View view, int newColor) {
    Drawable drawable = view.getBackground();
    if (drawable instanceof GradientDrawable) {
        GradientDrawable gDrawable = (GradientDrawable) drawable;
        gDrawable.setColor(newColor);//  w w  w.j  a  v a2  s  .  c  om
    } else if (drawable instanceof StateListDrawable) {
        StateListDrawable listDrawable = (StateListDrawable) drawable;

        DrawableContainer.DrawableContainerState drawableContainerState = (DrawableContainer.DrawableContainerState) listDrawable
                .getConstantState();

        int c = drawableContainerState.getChildCount();
        Drawable[] children = drawableContainerState.getChildren();

        for (int i = 0; i < c; ++i) {
            Drawable child = children[i];
            int[] states = child.getState();

            if (child instanceof GradientDrawable) {
                GradientDrawable gChild = (GradientDrawable) child;

                if (containsState(states, android.R.attr.state_pressed)) {
                    gChild.setColor(darkenColor(newColor));
                } else {
                    gChild.setColor(newColor);
                }
            } else if (child instanceof ColorDrawable) {
            }
        }
    }
}