Example usage for android.graphics.drawable ColorDrawable ColorDrawable

List of usage examples for android.graphics.drawable ColorDrawable ColorDrawable

Introduction

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

Prototype

public ColorDrawable(@ColorInt int color) 

Source Link

Document

Creates a new ColorDrawable with the specified color.

Usage

From source file:Main.java

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static void setActionBar(Activity activity, int background, String title) {

    // Action Bar Icon
    activity.getActionBar()//from   w w w . j ava  2s .  c  o m
            .setIcon(new ColorDrawable(activity.getResources().getColor(android.R.color.transparent)));

    // Action Bar Title
    activity.getActionBar().setTitle(title);

    // Action Bar Background
    Drawable draw;
    if (android.os.Build.VERSION.SDK_INT >= 21) {
        draw = activity.getResources().getDrawable(background, activity.getTheme());
        activity.getActionBar().setBackgroundDrawable(draw);
    } else {
        draw = activity.getResources().getDrawable(background);
        activity.getActionBar().setBackgroundDrawable(draw);
    }
}

From source file:com.mattermost.service.Promise.java

public static PopupWindow show() {
    Activity currentActivity = AppActivity.current;
    PopupWindow pw = new PopupWindow(currentActivity);
    pw.setFocusable(false);/*from  ww w . j av a  2 s  .  c  om*/
    pw.setBackgroundDrawable(new ColorDrawable(0));
    ImageView img = new ImageView(currentActivity);
    pw.setContentView(img);
    View view = currentActivity.getWindow().getDecorView();
    pw.setWidth(60);
    pw.setHeight(60);
    AnimatedCircleDrawable a = new AnimatedCircleDrawable(Color.RED, 5, Color.TRANSPARENT, 30);
    img.setImageDrawable(a);
    pw.showAtLocation(view, Gravity.LEFT | Gravity.TOP, view.getWidth() / 2 - 30, view.getHeight() / 2 - 30);
    a.start();

    return pw;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Drawable getRippleDrawable(int pressedColor, Integer normalColor) {
    ColorStateList colorStateList = getPressedColorSelector(pressedColor);
    Drawable mask, content = null;/*from  w  w  w. j  av  a  2s  .co m*/

    if (null == normalColor) {
        mask = new ShapeDrawable();
    } else {
        content = new ColorDrawable(normalColor);
        mask = getRippleMask(Color.WHITE);
    }
    return new RippleDrawable(colorStateList, content, mask);
}

From source file:Main.java

public static Drawable createRippleDrawable(final View v, final int color, Drawable pressed) {

    Drawable drawable = v.getBackground();

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        if (drawable instanceof RippleDrawable) {
            drawable = ((RippleDrawable) drawable).getDrawable(0);

            RippleDrawable rippleDrawable = new RippleDrawable(new ColorStateList(
                    new int[][] { new int[] { android.R.attr.state_pressed }, new int[] { 0 } },
                    new int[] { color, 0 }), drawable, null);

            return rippleDrawable;
        } else {//  w w w . java2 s .  c o  m
            if (drawable == null) {
                drawable = new ColorDrawable(0);
                v.setBackground(drawable);
                RippleDrawable rippleDrawable = new RippleDrawable(new ColorStateList(
                        new int[][] { new int[] { android.R.attr.state_pressed }, new int[] { 0 } },
                        new int[] { color, 0 }), drawable, new ColorDrawable(0xffffffff));
                return rippleDrawable;
            } else {
                RippleDrawable rippleDrawable = new RippleDrawable(new ColorStateList(
                        new int[][] { new int[] { android.R.attr.state_pressed }, new int[] { 0 } },
                        new int[] { color, 0 }), drawable, null);
                return rippleDrawable;
            }
        }
    } else {

        if (drawable == null) {
            drawable = new ColorDrawable(color);
        }

        if (pressed == null) {
            pressed = drawable;
        }

        StateListDrawable sld = new StateListDrawable();
        sld.addState(new int[] { android.R.attr.state_pressed, }, pressed);

        if (v.getBackground() != null) {
            sld.addState(new int[] { 0, }, drawable);
        }

        return sld;
    }
}

From source file:Main.java

/**
 * Returns a new ColorDrawable for the given color
 *
 * @param color the color//from   www  . j  av  a 2  s  . co  m
 * @return a new ColorDrawable that represents the given color.
 */
public static Drawable color(int color) {
    return new ColorDrawable(color);
}

From source file:Main.java

/**
 * To state list drawable state list drawable.
 *
 * @param normalColor  the normal color/*from   w  w w .ja v a 2s.  com*/
 * @param pressedColor the pressed color
 * @param focusedColor the focused color
 * @param unableColor  the unable color
 * @return the state list drawable
 */
public static StateListDrawable toStateListDrawable(int normalColor, int pressedColor, int focusedColor,
        int unableColor) {
    StateListDrawable drawable = new StateListDrawable();
    Drawable normal = new ColorDrawable(normalColor);
    Drawable pressed = new ColorDrawable(pressedColor);
    Drawable focused = new ColorDrawable(focusedColor);
    Drawable unable = new ColorDrawable(unableColor);
    drawable.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
    drawable.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused);
    drawable.addState(new int[] { android.R.attr.state_enabled }, normal);
    drawable.addState(new int[] { android.R.attr.state_focused }, focused);
    drawable.addState(new int[] { android.R.attr.state_window_focused }, unable);
    drawable.addState(new int[] {}, normal);
    return drawable;
}

From source file:Main.java

public static void traverseAndRecolor(View root, int color, boolean withStates,
        boolean setClickableItemBackgrounds) {
    Context context = root.getContext();

    if (setClickableItemBackgrounds && root.isClickable()) {
        StateListDrawable selectableItemBackground = new StateListDrawable();
        selectableItemBackground.addState(new int[] { android.R.attr.state_pressed },
                new ColorDrawable((color & 0xffffff) | 0x33000000));
        selectableItemBackground.addState(new int[] { android.R.attr.state_focused },
                new ColorDrawable((color & 0xffffff) | 0x44000000));
        selectableItemBackground.addState(new int[] {}, null);
        root.setBackground(selectableItemBackground);
    }//  www.j ava  2 s.co  m

    if (root instanceof ViewGroup) {
        ViewGroup parent = (ViewGroup) root;
        for (int i = 0; i < parent.getChildCount(); i++) {
            traverseAndRecolor(parent.getChildAt(i), color, withStates, setClickableItemBackgrounds);
        }

    } else if (root instanceof ImageView) {
        ImageView imageView = (ImageView) root;
        Drawable sourceDrawable = imageView.getDrawable();
        if (withStates && sourceDrawable != null && sourceDrawable instanceof BitmapDrawable) {
            imageView.setImageDrawable(
                    makeRecoloredDrawable(context, (BitmapDrawable) sourceDrawable, color, true));
        } else {
            imageView.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
        }

    } else if (root instanceof TextView) {
        TextView textView = (TextView) root;
        if (withStates) {
            int sourceColor = textView.getCurrentTextColor();
            ColorStateList colorStateList = new ColorStateList(
                    new int[][] { new int[] { android.R.attr.state_pressed },
                            new int[] { android.R.attr.state_focused }, new int[] {} },
                    new int[] { sourceColor, sourceColor, color });
            textView.setTextColor(colorStateList);
        } else {
            textView.setTextColor(color);
        }

    } else if (root instanceof AnalogClock) {
        AnalogClock analogClock = (AnalogClock) root;
        try {
            Field hourHandField = AnalogClock.class.getDeclaredField("mHourHand");
            hourHandField.setAccessible(true);
            Field minuteHandField = AnalogClock.class.getDeclaredField("mMinuteHand");
            minuteHandField.setAccessible(true);
            Field dialField = AnalogClock.class.getDeclaredField("mDial");
            dialField.setAccessible(true);
            BitmapDrawable hourHand = (BitmapDrawable) hourHandField.get(analogClock);
            if (hourHand != null) {
                Drawable d = makeRecoloredDrawable(context, hourHand, color, withStates);
                d.setCallback(analogClock);
                hourHandField.set(analogClock, d);
            }
            BitmapDrawable minuteHand = (BitmapDrawable) minuteHandField.get(analogClock);
            if (minuteHand != null) {
                Drawable d = makeRecoloredDrawable(context, minuteHand, color, withStates);
                d.setCallback(analogClock);
                minuteHandField.set(analogClock, d);
            }
            BitmapDrawable dial = (BitmapDrawable) dialField.get(analogClock);
            if (dial != null) {
                Drawable d = makeRecoloredDrawable(context, dial, color, withStates);
                d.setCallback(analogClock);
                dialField.set(analogClock, d);
            }
        } catch (NoSuchFieldException ignored) {
        } catch (IllegalAccessException ignored) {
        } catch (ClassCastException ignored) {
        } // TODO: catch all exceptions?
    }
}

From source file:com.affectiva.affdexme.SettingsActivity.java

@Override
public void onCreate(Bundle savedBundleInstance) {
    super.onCreate(savedBundleInstance);
    ActionBar actionBar = getActionBar();
    if (actionBar != null) {
        actionBar.setIcon(new ColorDrawable(
                ContextCompat.getColor(getApplicationContext(), R.color.transparent_overlay)));
    }/*from   w w w.  j a v  a2 s.  co m*/
}

From source file:Main.java

@NonNull
private static StateListDrawable getStateListDrawable(int normalColor, int pressedColor) {
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(pressedColor));
    states.addState(new int[] { android.R.attr.state_focused }, new ColorDrawable(pressedColor));
    states.addState(new int[] { android.R.attr.state_activated }, new ColorDrawable(pressedColor));
    states.addState(new int[] {}, new ColorDrawable(normalColor));
    states.addState(StateSet.WILD_CARD, new ColorDrawable(normalColor));
    return states;
}

From source file:Main.java

@NonNull
private static ColorDrawable getColorDrawableFromColor(int color) {
    return new ColorDrawable(color);
}