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 Drawable cornerDrawable(final int bgColor, float[] cornerradius, int borderwidth,
        int bordercolor) {
    final GradientDrawable bg = new GradientDrawable();
    bg.setCornerRadii(cornerradius);//from  ww  w. j ava  2  s .com
    bg.setStroke(borderwidth, bordercolor);
    bg.setColor(bgColor);

    return bg;
}

From source file:Main.java

public static Drawable createDrawableBackground(int color, int cornerRadius, boolean hasBorder,
        int borderThickness, int borderColor) {
    GradientDrawable drawable = new GradientDrawable();
    drawable.setShape(GradientDrawable.RECTANGLE);
    if (hasBorder)
        drawable.setStroke(borderThickness, borderColor);
    drawable.setCornerRadius(cornerRadius);
    drawable.setColor(color);
    return drawable;
}

From source file:com.tr4android.support.extension.picker.PickerThemeUtils.java

public static Drawable getHeaderBackground(Context context, @ColorInt int color) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        // Create a background drawable with 2dp corners
        GradientDrawable drawable = (GradientDrawable) ContextCompat.getDrawable(context,
                R.drawable.picker_header_background);
        drawable.setColor(color);
        return drawable;
    } else {/*from   ww w . j  av a 2  s.  c  o  m*/
        return new ColorDrawable(color);
    }
}

From source file:Main.java

/**
 * Set the background of a {@link android.view.View} to the specified color, with a darker version of the
 * color as a border./*  ww  w  .  j ava 2  s . c  o  m*/
 */
public static void setViewBackground(View view, int color) {
    Resources r = view.getResources();

    Drawable currentDrawable = view.getBackground();
    GradientDrawable backgroundDrawable;
    if (currentDrawable != null && currentDrawable instanceof GradientDrawable) {
        // Reuse drawable
        backgroundDrawable = (GradientDrawable) currentDrawable;
    } else {
        backgroundDrawable = new GradientDrawable();
    }

    int darkenedColor = darkenColor(color);

    backgroundDrawable.setColor(color);
    backgroundDrawable.setStroke(
            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics()),
            darkenedColor);

    view.setBackgroundDrawable(backgroundDrawable);
}

From source file:com.esri.android.ecologicalmarineunitexplorer.bottomsheet.BottomSheetFragment.java

/**
 * Build a stateful drawable for a given EMU
 * @param emuName String representing name
 * @return StateListDrawable responsive to selected, pressed, and enabled states
 *//*  w w w  . j a v  a 2s  .  co  m*/
private static StateListDrawable buildStateList(final int emuName) {
    final StateListDrawable stateListDrawable = new StateListDrawable();

    final GradientDrawable defaultShape = new GradientDrawable();
    final int color = Color.parseColor(EmuHelper.getColorForEMUCluster(emuName));
    defaultShape.setColor(color);

    final GradientDrawable selectedPressShape = new GradientDrawable();
    selectedPressShape.setColor(color);
    selectedPressShape.setStroke(5, Color.parseColor("#f4f442"));

    stateListDrawable.addState(new int[] { android.R.attr.state_pressed }, selectedPressShape);
    stateListDrawable.addState(new int[] { android.R.attr.state_selected }, selectedPressShape);
    stateListDrawable.addState(new int[] { android.R.attr.state_enabled }, defaultShape);

    return stateListDrawable;
}

From source file:com.fast.access.kam.widget.colorpicker.dashclockpicker.ColorPickerDialogDash.java

private static void setColorViewValue(View view, int color) {
    if (view instanceof ImageView) {
        ImageView imageView = (ImageView) view;
        Resources res = imageView.getContext().getResources();

        Drawable currentDrawable = imageView.getDrawable();
        GradientDrawable colorChoiceDrawable;
        if (currentDrawable != null && currentDrawable instanceof GradientDrawable) {
            // Reuse drawable
            colorChoiceDrawable = (GradientDrawable) currentDrawable;
        } else {/*from   ww w  . j av  a 2 s  .com*/
            colorChoiceDrawable = new GradientDrawable();
            colorChoiceDrawable.setShape(GradientDrawable.OVAL);
        }

        // Set stroke to dark version of color
        int darkenedColor = Color.rgb(Color.red(color) * 192 / 256, Color.green(color) * 192 / 256,
                Color.blue(color) * 192 / 256);

        colorChoiceDrawable.setColor(color);
        colorChoiceDrawable.setStroke(
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, res.getDisplayMetrics()),
                darkenedColor);
        imageView.setImageDrawable(colorChoiceDrawable);

    } else if (view instanceof TextView) {
        ((TextView) view).setTextColor(color);
    }
}

From source file:org.smssecure.smssecure.preferences.ColorPreference.java

private static void setColorViewValue(View view, int color, boolean selected) {
    if (view instanceof ImageView) {
        ImageView imageView = (ImageView) view;
        Resources res = imageView.getContext().getResources();

        Drawable currentDrawable = imageView.getDrawable();
        GradientDrawable colorChoiceDrawable;
        if (currentDrawable instanceof GradientDrawable) {
            // Reuse drawable
            colorChoiceDrawable = (GradientDrawable) currentDrawable;
        } else {//from   w w  w  .  jav  a  2  s.  c om
            colorChoiceDrawable = new GradientDrawable();
            colorChoiceDrawable.setShape(GradientDrawable.OVAL);
        }

        // Set stroke to dark version of color
        //      int darkenedColor = Color.rgb(
        //          Color.red(color) * 192 / 256,
        //          Color.green(color) * 192 / 256,
        //          Color.blue(color) * 192 / 256);

        colorChoiceDrawable.setColor(color);
        //      colorChoiceDrawable.setStroke((int) TypedValue.applyDimension(
        //          TypedValue.COMPLEX_UNIT_DIP, 2, res.getDisplayMetrics()), darkenedColor);

        Drawable drawable = colorChoiceDrawable;
        if (selected) {
            BitmapDrawable checkmark = (BitmapDrawable) res.getDrawable(R.drawable.check);
            checkmark.setGravity(Gravity.CENTER);
            drawable = new LayerDrawable(new Drawable[] { colorChoiceDrawable, checkmark });
        }

        imageView.setImageDrawable(drawable);

    } else if (view instanceof TextView) {
        ((TextView) view).setTextColor(color);
    }
}

From source file:com.tingtingapps.securesms.preferences.ColorPreference.java

private static void setColorViewValue(View view, int color, boolean selected) {
    if (view instanceof ImageView) {
        ImageView imageView = (ImageView) view;
        Resources res = imageView.getContext().getResources();

        Drawable currentDrawable = imageView.getDrawable();
        GradientDrawable colorChoiceDrawable;
        if (currentDrawable instanceof GradientDrawable) {
            // Reuse drawable
            colorChoiceDrawable = (GradientDrawable) currentDrawable;
        } else {//from ww w.  jav  a 2 s .c  om
            colorChoiceDrawable = new GradientDrawable();
            colorChoiceDrawable.setShape(GradientDrawable.OVAL);
        }

        // Set stroke to dark version of color
        //      int darkenedColor = Color.rgb(
        //          Color.red(color) * 192 / 256,
        //          Color.green(color) * 192 / 256,
        //          Color.blue(color) * 192 / 256);

        colorChoiceDrawable.setColor(color);
        //      colorChoiceDrawable.setStroke((int) TypedValue.applyDimension(
        //          TypedValue.COMPLEX_UNIT_DIP, 2, res.getDisplayMetrics()), darkenedColor);

        Drawable drawable = colorChoiceDrawable;
        if (selected) {
            BitmapDrawable checkmark = (BitmapDrawable) res
                    .getDrawable(com.tingtingapps.securesms.R.drawable.check);
            checkmark.setGravity(Gravity.CENTER);
            drawable = new LayerDrawable(new Drawable[] { colorChoiceDrawable, checkmark });
        }

        imageView.setImageDrawable(drawable);

    } else if (view instanceof TextView) {
        ((TextView) view).setTextColor(color);
    }
}

From source file:com.mobicage.rogerthat.util.ui.UIUtils.java

public static void setBackgroundColor(View view, int backgroundColor) {
    Drawable background = view.getBackground();
    if (background instanceof ShapeDrawable) {
        ShapeDrawable shapeDrawable = (ShapeDrawable) background;
        shapeDrawable.getPaint().setColor(backgroundColor);
    } else if (background instanceof GradientDrawable) {
        GradientDrawable gradientDrawable = (GradientDrawable) background;
        gradientDrawable.setColor(backgroundColor);
    } else if (background instanceof ColorDrawable) {
        // alpha value may need to be set again after this call
        ColorDrawable colorDrawable = (ColorDrawable) background;
        colorDrawable.setColor(backgroundColor);
    } else {/*  w  w  w . j ava 2  s  .c o m*/
        L.e("Unknown background type to set color on: " + view.getClass());
    }
}

From source file:com.forrestguice.suntimeswidget.SuntimesUtils.java

public static Drawable tintDrawable(GradientDrawable drawable, int fillColor, int strokeColor,
        int strokePixels) {
    drawable.setStroke(strokePixels, strokeColor);
    drawable.setColor(fillColor);
    return drawable;
}