Example usage for android.graphics.drawable StateListDrawable addState

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

Introduction

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

Prototype

public void addState(int[] stateSet, Drawable drawable) 

Source Link

Document

Add a new image/string ID to the set of images.

Usage

From source file:com.aqtc.bmobnews.util.ResourcesUtils.java

/**
 * ColorStateList// w  ww.  ja v a  2s. c om
 * create a StateListDrawable instance
 *
 * @param context context
 * @param normalRes normalRes
 * @param pressedRes pressedRes
 * @param focusedRes focusedRes
 * @param unableRes unableRes
 * @return StateListDrawable
 */
public static StateListDrawable createStateListDrawable(Context context, @DrawableRes int normalRes,
        @DrawableRes int pressedRes, @DrawableRes int focusedRes, @DrawableRes int unableRes) {
    StateListDrawable stateListDrawable = new StateListDrawable();
    stateListDrawable.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled },
            getDrawable(context, pressedRes));
    stateListDrawable.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused },
            getDrawable(context, focusedRes));
    stateListDrawable.addState(new int[] { android.R.attr.state_enabled }, getDrawable(context, normalRes));
    stateListDrawable.addState(new int[] { android.R.attr.state_focused }, getDrawable(context, focusedRes));
    stateListDrawable.addState(new int[] { android.R.attr.state_window_focused },
            getDrawable(context, unableRes));
    stateListDrawable.addState(new int[] {}, getDrawable(context, normalRes));
    return stateListDrawable;
}

From source file:com.sbgapps.simplenumberpicker.utils.ThemeUtil.java

public static StateListDrawable makeSelector(Context context, int drawableResId, int color) {
    StateListDrawable res = new StateListDrawable();
    res.setExitFadeDuration(50);//from  ww  w. ja v a2 s.c om
    Drawable drawable = ContextCompat.getDrawable(context, drawableResId);
    DrawableCompat.setTint(drawable, color);
    res.addState(new int[] { android.R.attr.state_enabled }, drawable);
    drawable = ContextCompat.getDrawable(context, drawableResId);
    DrawableCompat.setTint(drawable, color & 0x40FFFFFF);
    res.addState(new int[] { -android.R.attr.state_enabled }, drawable);
    return res;
}

From source file:com.flexible.flexibleadapter.utils.DrawableUtils.java

private static StateListDrawable getStateListDrawable(@ColorInt int normalColor, @ColorInt int pressedColor) {
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_activated }, getColorDrawable(pressedColor));
    states.addState(new int[] {}, getColorDrawable(normalColor));
    // Animating across states.
    // It seems item background is lost on scrolling out of the screen, 21 <= API <= 23
    if (!Utils.hasLollipop() || Utils.hasNougat()) {
        int duration = 200; //android.R.integer.config_shortAnimTime
        states.setEnterFadeDuration(duration);
        states.setExitFadeDuration(duration);
    }/*from w  ww .  j  a va  2 s  .  c om*/
    return states;
}

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);
    }/*from w  w w .ja  v  a2s. 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:Main.java

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:com.tr4android.support.extension.internal.AccountAdapter.java

private static void setupCheckBox(CheckBox checkBox) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        checkBox.setButtonDrawable(R.drawable.btn_checkbox_circle);
        checkBox.setBackgroundResource(R.drawable.btn_checkbox_circle_background);
    } else {//  w w w.  j  a  v  a  2  s  .  c  o  m
        Context context = checkBox.getContext();
        AppCompatDrawableManager dm = AppCompatDrawableManager.get();

        StateListDrawable button = new StateListDrawable();
        button.addState(new int[] { android.R.attr.state_checked },
                dm.getDrawable(context, R.drawable.ic_checkbox_circle_checked));
        button.addState(new int[] {}, dm.getDrawable(context, R.drawable.ic_checkbox_circle_unchecked));
        ColorStateList buttonTint = new ColorStateList(new int[][] { // states
                new int[] { android.R.attr.state_checked }, new int[] {} // state_default
        }, new int[] { // colors
                ThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated),
                ThemeUtils.getThemeAttrColor(context, R.attr.colorControlNormal) });
        Drawable buttonCompat = DrawableCompat.wrap(button);
        DrawableCompat.setTintList(buttonCompat, buttonTint);
        checkBox.setButtonDrawable(buttonCompat);

        ShapeDrawable background = new ShapeDrawable(new OvalShape());
        int backgroundTint = ThemeUtils.getThemeAttrColor(context, android.R.attr.colorBackground);
        Drawable backgroundCompat = DrawableCompat.wrap(background);
        DrawableCompat.setTint(backgroundCompat, backgroundTint);
        ViewCompatUtils.setBackground(checkBox, backgroundCompat);
    }
}

From source file:arun.com.chromer.util.ColorUtil.java

@NonNull
public static Drawable getRippleDrawableCompat(final @ColorInt int color) {
    if (Utils.isLollipopAbove()) {
        return new RippleDrawable(ColorStateList.valueOf(color), null, null);
    }//from  ww w .  j  av a2s  .co  m
    int translucentColor = ColorUtils.setAlphaComponent(color, 0x44);
    StateListDrawable stateListDrawable = new StateListDrawable();
    int[] states = new int[] { android.R.attr.state_pressed };
    stateListDrawable.addState(states, new ColorDrawable(translucentColor));
    return stateListDrawable;
}

From source file:Main.java

/**
 * To state list drawable state list drawable.
 *
 * @param normalColor  the normal color/*from www  . j  ava  2  s .  c o  m*/
 * @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:com.appeaser.sublimepickerlibrary.utilities.SUtils.java

private static Drawable createButtonNormalBg(Context context, int colorControlHighlight) {
    StateListDrawable sld = new StateListDrawable();
    sld.addState(new int[] { android.R.attr.state_pressed }, createButtonShape(context, colorControlHighlight));
    sld.addState(new int[] {}, new ColorDrawable(Color.TRANSPARENT));
    return sld;/*from  w  w  w  . j  a  v a  2 s  .  c  o  m*/
}

From source file:com.appeaser.sublimepickerlibrary.utilities.SUtils.java

private static Drawable createImageViewNormalBg(int colorControlHighlight) {
    StateListDrawable sld = new StateListDrawable();
    sld.addState(new int[] { android.R.attr.state_pressed }, createImageViewShape(colorControlHighlight));
    sld.addState(new int[] {}, new ColorDrawable(Color.TRANSPARENT));
    return sld;/*from  w w  w  .ja va2s  . c om*/
}