Example usage for android.graphics.drawable GradientDrawable setColor

List of usage examples for android.graphics.drawable GradientDrawable setColor

Introduction

In this page you can find the example usage for android.graphics.drawable GradientDrawable setColor.

Prototype

public void setColor(@Nullable ColorStateList colorStateList) 

Source Link

Document

Changes this drawable to use a single color state list instead of a gradient.

Usage

From source file:Main.java

public static GradientDrawable createBackgroundRandom() {
    Random rnd = new Random();
    int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    GradientDrawable gd = new GradientDrawable();
    gd.setColor(color);
    gd.setCornerRadius(5);/* w  w w.  j  a v a  2 s  .  c om*/
    gd.setAlpha(90);
    return gd;
}

From source file:Main.java

/**
 *
 * @param radius//  w w  w  .  j  av  a2s  . com
 * @return
 * @deprecated Change to use ViewUtils.
 */
@Deprecated
public static GradientDrawable getShapeDrawable(float radius) {
    GradientDrawable shape = new GradientDrawable();
    shape.setColor(Color.TRANSPARENT);
    shape.setCornerRadius(radius);
    return shape;
}

From source file:Main.java

public static void makeRoundCorner(View v, int color, int radius) {
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setColor(color);
    gradientDrawable.setCornerRadius(radius);
    if (Build.VERSION.SDK_INT < 16)
        v.setBackgroundDrawable(gradientDrawable);
    else//from  w  ww.  j a  v  a 2  s .c o m
        v.setBackground(gradientDrawable);
}

From source file:Main.java

public static Drawable createRoundRectShape(int corner, int color) {
    GradientDrawable drawable = new GradientDrawable();
    drawable.setCornerRadius(corner);//from ww w  .j  av  a2 s. c om
    drawable.setColor(color);
    return drawable;
}

From source file:Main.java

public static GradientDrawable getDrawable(int radius, int fillColor, int width, int strokeColor) {
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setCornerRadius(radius);
    gradientDrawable.setColor(fillColor);
    gradientDrawable.setStroke(width, strokeColor);
    return gradientDrawable;
}

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);
    } else if (drawable instanceof StateListDrawable) {
        StateListDrawable listDrawable = (StateListDrawable) drawable;

        DrawableContainer.DrawableContainerState drawableContainerState = (DrawableContainer.DrawableContainerState) listDrawable
                .getConstantState();//from w  w  w .  jav  a 2  s. c  om

        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) {
            }
        }
    }
}

From source file:Main.java

public static Drawable cornerDrawable(final int bgColor, float[] cornerradius) {
    final GradientDrawable bg = new GradientDrawable();
    bg.setCornerRadii(cornerradius);/* w  ww . ja  va2  s .  c  o m*/
    bg.setColor(bgColor);

    return bg;
}

From source file:Main.java

public static Drawable cornerDrawable(final int bgColor, float cornerradius) {
    final GradientDrawable bg = new GradientDrawable();
    bg.setCornerRadius(cornerradius);//from   w  ww. j  a v  a 2  s .  c om
    bg.setColor(bgColor);

    return bg;
}

From source file:Main.java

public static Drawable createRandomColorShapeDrawable() {
    GradientDrawable drawable = new GradientDrawable();
    drawable.setShape(GradientDrawable.RECTANGLE);
    drawable.setCornerRadius(5);/*  w  w w .  ja v  a  2s  .  c o m*/
    drawable.setColor(createRandomColor());
    return drawable;
}

From source file:Main.java

public static GradientDrawable getShape(float radius, int color) {
    GradientDrawable gd = new GradientDrawable();
    //        gd.setShape(GradientDrawable.OVAL); The larger the radius, the more round the radius
    gd.setCornerRadius(radius);/*from  w w  w.  j  a v a  2  s. c  o m*/
    gd.setColor(color);
    return gd;
}