Example usage for android.content.res ColorStateList ColorStateList

List of usage examples for android.content.res ColorStateList ColorStateList

Introduction

In this page you can find the example usage for android.content.res ColorStateList ColorStateList.

Prototype

public ColorStateList(int[][] states, @ColorInt int[] colors) 

Source Link

Document

Creates a ColorStateList that returns the specified mapping from states to colors.

Usage

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. java  2s . co 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

public static ColorStateList createColorStateList(int colorNormal, int colorPressed, int colorFocused,
        int colorDisable) {
    int[] colors = new int[] { colorPressed, colorFocused, colorNormal, colorFocused, colorDisable,
            colorNormal };/*  ww  w.  j  av  a2s.c o  m*/
    int[][] states = new int[6][];
    states[0] = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };
    states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused };
    states[2] = new int[] { android.R.attr.state_enabled };
    states[3] = new int[] { android.R.attr.state_focused };
    states[4] = new int[] { android.R.attr.state_window_focused };
    states[5] = new int[] {};
    ColorStateList colorList = new ColorStateList(states, colors);
    return colorList;
}

From source file:Main.java

/**
 * Creates a color states-list.//from w w w.j a  va2s .  co  m
 * 
 * @param color
 * @return A state-list
 */
public static ColorStateList createColorStateList(int color) {
    // @formatter:off
    // FIXME - This is buggy. The minute we set the color in, the button is no longer clickable....
    //   [[-16842910], [16842908, -16842910], [16842919], [16842913], [16842908], []]
    //  [-2147483648,      -2147483648,          -1,         -1,        -1,   -16777216]
    return new ColorStateList(
            new int[][] { new int[] { -android.R.attr.state_enabled },
                    new int[] { android.R.attr.state_focused, -android.R.attr.state_enabled },
                    new int[] { android.R.attr.state_pressed }, new int[] { android.R.attr.state_selected },
                    new int[] { android.R.attr.state_focused }, new int[0] },
            new int[] { Integer.MIN_VALUE, // !enabled
                    Integer.MIN_VALUE, // focused & !enabled
                    Color.WHITE, // pressed
                    Color.WHITE, // selected
                    Color.WHITE, // focused
                    color });

    // @formatter:on
}

From source file:org.wheelmap.android.utils.ViewTool.java

public static void setBackgroundTint(View v, @ColorInt int color) {
    ColorStateList csl = new ColorStateList(new int[][] { new int[0] }, new int[] { color });
    setBackgroundTintList(v, csl);/*www.ja  v a 2 s .  com*/
}

From source file:Main.java

private static ColorStateList getPressedColorSelector(int pressedColor) {
    return new ColorStateList(new int[][] { new int[] {} }, new int[] { pressedColor });
}

From source file:android.support.v7.internal.widget.ThemeUtils.java

static ColorStateList createDisabledStateList(int textColor, int disabledTextColor) {
    // Now create a new ColorStateList with the default color, and the new disabled
    // color// w ww  .j  a  va  2  s  . c om
    final int[][] states = new int[2][];
    final int[] colors = new int[2];
    int i = 0;

    // Disabled state
    states[i] = DISABLED_STATE_SET;
    colors[i] = disabledTextColor;
    i++;

    // Default state
    states[i] = EMPTY_STATE_SET;
    colors[i] = textColor;
    i++;

    return new ColorStateList(states, colors);
}

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

public static ColorStateList getHeaderTextColorStateList(Context context) {
    return new ColorStateList(new int[][] { // states
            new int[] { android.R.attr.state_selected }, new int[] {} // state_default
    }, new int[] { // colors
            ContextCompat.getColor(context, R.color.abc_primary_text_material_dark),
            ContextCompat.getColor(context, R.color.abc_secondary_text_material_dark) });
}

From source file:android.support.v7.widget.ThemeUtils.java

public static ColorStateList createDisabledStateList(int textColor, int disabledTextColor) {
    // Now create a new ColorStateList with the default color, and the new disabled
    // color/*  w ww  . j a  v  a 2  s.  c om*/
    final int[][] states = new int[2][];
    final int[] colors = new int[2];
    int i = 0;

    // Disabled state
    states[i] = DISABLED_STATE_SET;
    colors[i] = disabledTextColor;
    i++;

    // Default state
    states[i] = EMPTY_STATE_SET;
    colors[i] = textColor;
    i++;

    return new ColorStateList(states, colors);
}

From source file:com.github.dfa.diaspora_android.ui.theme.ThemeHelper.java

public static void updateCheckBoxColor(CheckBox checkBox) {
    if (checkBox != null) {
        int states[][] = { { android.R.attr.state_checked }, {} };
        int colors[] = { ThemeHelper.getAccentColor(), getNeutralGreyColor() };
        CompoundButtonCompat.setButtonTintList(checkBox, new ColorStateList(states, colors));
    }//from w  w  w .  ja  v a2s .  c o  m
}

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);
    }//w  w w .j  a  va2  s.c o 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?
    }
}