Example usage for android.content.res ColorStateList isStateful

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

Introduction

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

Prototype

@Override
public boolean isStateful() 

Source Link

Document

Indicates whether this color state list contains at least one state spec and the first spec is not empty (e.g.

Usage

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

static int getDisabledThemeAttrColor(Context context, int attr) {
    final ColorStateList csl = getThemeAttrColorStateList(context, attr);
    if (csl != null && csl.isStateful()) {
        // If the CSL is stateful, we'll assume it has a disabled state and use it
        return csl.getColorForState(DISABLED_STATE_SET, csl.getDefaultColor());
    } else {/*w  w  w .  ja  v a 2  s . c  om*/
        // Else, we'll generate the color using disabledAlpha from the theme

        final TypedValue tv = getTypedValue();
        // Now retrieve the disabledAlpha value from the theme
        context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, tv, true);
        final float disabledAlpha = tv.getFloat();

        return getThemeAttrColor(context, attr, disabledAlpha);
    }
}

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

public static int getDisabledThemeAttrColor(Context context, int attr) {
    final ColorStateList csl = getThemeAttrColorStateList(context, attr);
    if (csl != null && csl.isStateful()) {
        // If the CSL is stateful, we'll assume it has a disabled state and use it
        return csl.getColorForState(DISABLED_STATE_SET, csl.getDefaultColor());
    } else {//from  w w  w  . j  a v a 2 s.co  m
        // Else, we'll generate the color using disabledAlpha from the theme

        final TypedValue tv = getTypedValue();
        // Now retrieve the disabledAlpha value from the theme
        context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, tv, true);
        final float disabledAlpha = tv.getFloat();

        return getThemeAttrColor(context, attr, disabledAlpha);
    }
}

From source file:com.bilibili.magicasakura.utils.ThemeUtils.java

public static int getDisabledThemeAttrColor(Context context, @AttrRes int attr) {
    final ColorStateList csl = getThemeAttrColorStateList(context, attr);
    if (csl != null && csl.isStateful()) {
        // If the CSL is stateful, we'll assume it has a disabled state and use it
        return csl.getColorForState(DISABLED_STATE_SET, csl.getDefaultColor());
    } else {// ww  w.j a  v  a 2 s.c o m
        // Else, we'll generate the color using disabledAlpha from the theme

        final TypedValue tv = getTypedValue();
        // Now retrieve the disabledAlpha value from the theme
        context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, tv, true);
        final float disabledAlpha = tv.getFloat();

        return getThemeAttrColor(context, attr, disabledAlpha);
    }
}

From source file:com.bilibili.magicasakura.utils.ThemeUtils.java

public static ColorStateList getThemeColorStateList(Context context, ColorStateList origin) {
    if (origin == null)
        return null;

    if (origin.isStateful()) {
        TintInfo tintInfo = parseColorStateList(origin);
        if (tintInfo == null || tintInfo.isInvalid()) {
            return origin;
        }//from www .  jav a  2s  .co m

        int[] newColors;
        int[][] newStates;
        int index = 0;
        boolean hasDisableColor = StateSet.stateSetMatches(tintInfo.mTintStates[0], DISABLED_STATE_SET);
        if (!hasDisableColor) {
            newStates = new int[tintInfo.mTintStates.length + 1][];
            newColors = new int[tintInfo.mTintStates.length + 1];
            newStates[index] = DISABLED_STATE_SET;
            newColors[index] = getDisabledThemeAttrColor(context, R.attr.themeColorSecondary);
            index++;
        } else {
            newStates = new int[tintInfo.mTintStates.length][];
            newColors = new int[tintInfo.mTintStates.length];
        }

        for (int i = 0; i < tintInfo.mTintStates.length; i++) {
            newStates[index] = tintInfo.mTintStates[i];
            newColors[index] = replaceColor(context, tintInfo.mTintColors[i]);
            index++;
        }
        return new ColorStateList(newStates, newColors);
    }
    return ColorStateList.valueOf(replaceColor(context, origin.getDefaultColor()));
}

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

public TintButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    if (TintManager.SHOULD_BE_USED) {
        TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs, TINT_ATTRS, defStyleAttr,
                0);/*w  w w .  jav  a 2  s .co m*/
        if (a.hasValue(0)) {
            setSupportBackgroundTintList(a.getTintManager().getColorStateList(a.getResourceId(0, -1)));
        }
        a.recycle();
    }

    final ColorStateList textColors = getTextColors();
    if (textColors != null && !textColors.isStateful()) {
        // If we have a ColorStateList which isn't stateful, create one which includes
        // a disabled state

        final int disabledTextColor;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            // Pre-Lollipop, we will use textColorSecondary with android:disabledAlpha
            // applied
            disabledTextColor = ThemeUtils.getDisabledThemeAttrColor(context,
                    android.R.attr.textColorSecondary);
        } else {
            // With certain styles on Lollipop, there is a StateListAnimator which sets
            // an alpha on the whole view, so we don't need to apply disabledAlpha to
            // textColorSecondary
            disabledTextColor = ThemeUtils.getThemeAttrColor(context, android.R.attr.textColorSecondary);
        }

        setTextColor(ThemeUtils.createDisabledStateList(textColors.getDefaultColor(), disabledTextColor));
    }
}

From source file:com.fenlisproject.elf.core.widget.ExtendedButton.java

public ExtendedButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    if (TintManager.SHOULD_BE_USED) {
        TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs, TINT_ATTRS, defStyleAttr,
                0);//from   w w w. j a  v a 2  s . c om
        if (a.hasValue(0)) {
            ColorStateList tint = a.getTintManager().getTintList(a.getResourceId(0, -1));
            if (tint != null) {
                setSupportBackgroundTintList(tint);
            }
        }
        a.recycle();
    }

    // First read the TextAppearance style id
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppCompatTextView, defStyleAttr, 0);
    final int ap = a.getResourceId(R.styleable.AppCompatTextView_android_textAppearance, -1);
    a.recycle();

    // Now check TextAppearance's textAllCaps value
    if (ap != -1) {
        TypedArray appearance = context.obtainStyledAttributes(ap, R.styleable.TextAppearance);
        if (appearance.hasValue(R.styleable.TextAppearance_textAllCaps)) {
            setAllCaps(appearance.getBoolean(R.styleable.TextAppearance_textAllCaps, false));
        }
        appearance.recycle();
    }

    // Now read the style's value
    a = context.obtainStyledAttributes(attrs, R.styleable.AppCompatTextView, defStyleAttr, 0);
    if (a.hasValue(R.styleable.AppCompatTextView_textAllCaps)) {
        setAllCaps(a.getBoolean(R.styleable.AppCompatTextView_textAllCaps, false));
    }
    a.recycle();

    final ColorStateList textColors = getTextColors();
    if (textColors != null && !textColors.isStateful()) {
        // If we have a ColorStateList which isn't stateful, create one which includes
        // a disabled state

        final int disabledTextColor;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            // Pre-Lollipop, we will use textColorSecondary with android:disabledAlpha
            // applied
            disabledTextColor = ThemeUtils.getDisabledThemeAttrColor(context,
                    android.R.attr.textColorSecondary);
        } else {
            // With certain styles on Lollipop, there is a StateListAnimator which sets
            // an alpha on the whole view, so we don't need to apply disabledAlpha to
            // textColorSecondary
            disabledTextColor = ThemeUtils.getThemeAttrColor(context, android.R.attr.textColorSecondary);
        }

        setTextColor(ThemeUtils.createDisabledStateList(textColors.getDefaultColor(), disabledTextColor));
    }
}

From source file:com.tencent.tws.assistant.widget.CheckBox.java

public void setTintButtonDrawable(ColorStateList colorStateList) {
    mIsAnimationButton = true;//from w w  w.j a  v a  2  s  .  com
    int colorControlNormal = 0;
    int colorControlDisabled = 0;
    int colorControlActivated = 0;
    int colorControlActivateDisabled = 0;
    if (colorStateList.isStateful()) {
        colorControlNormal = colorStateList.getColorForState(checkOffState, colorStateList.getDefaultColor());
        colorControlDisabled = colorStateList.getColorForState(checkOffDisableState,
                colorStateList.getDefaultColor());
        colorControlActivated = colorStateList.getColorForState(checkOnState, colorStateList.getDefaultColor());
        colorControlActivateDisabled = colorStateList.getColorForState(checkOnDisableState,
                colorStateList.getDefaultColor());
    }
    setTintButtonDrawable(colorControlNormal, colorControlDisabled, colorControlActivated,
            colorControlActivateDisabled);
}

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

private ColorStateList createSwitchThumbColorStateList() {
    final int[][] states = new int[3][];
    final int[] colors = new int[3];
    int i = 0;/*from ww w  . jav a2 s  .co m*/

    final ColorStateList thumbColor = getThemeAttrColorStateList(mContext, R.attr.colorSwitchThumbNormal);

    if (thumbColor != null && thumbColor.isStateful()) {
        // If colorSwitchThumbNormal is a valid ColorStateList, extract the default and
        // disabled colors from it

        // Disabled state
        states[i] = new int[] { -android.R.attr.state_enabled };
        colors[i] = thumbColor.getColorForState(states[i], 0);
        i++;

        states[i] = new int[] { android.R.attr.state_checked };
        colors[i] = getThemeAttrColor(mContext, R.attr.colorControlActivated);
        i++;

        // Default enabled state
        states[i] = new int[0];
        colors[i] = thumbColor.getDefaultColor();
        i++;
    } else {
        // Else we'll use an approximation using the default disabled alpha

        // Disabled state
        states[i] = new int[] { -android.R.attr.state_enabled };
        colors[i] = getDisabledThemeAttrColor(mContext, R.attr.colorSwitchThumbNormal);
        i++;

        states[i] = new int[] { android.R.attr.state_checked };
        colors[i] = getThemeAttrColor(mContext, R.attr.colorControlActivated);
        i++;

        // Default enabled state
        states[i] = new int[0];
        colors[i] = getThemeAttrColor(mContext, R.attr.colorSwitchThumbNormal);
        i++;
    }

    return new ColorStateList(states, colors);
}

From source file:com.max.library.view.v7.AppCompatDrawableManager.java

private ColorStateList createSwitchThumbColorStateList(Context context) {
    final int[][] states = new int[3][];
    final int[] colors = new int[3];
    int i = 0;//from w  w w.  j  a v  a  2 s. c  o  m

    final ColorStateList thumbColor = getThemeAttrColorStateList(context, R.attr.colorSwitchThumbNormal);

    if (thumbColor != null && thumbColor.isStateful()) {
        // If colorSwitchThumbNormal is a valid ColorStateList, extract the default and
        // disabled colors from it

        // Disabled state
        states[i] = ThemeUtils.DISABLED_STATE_SET;
        colors[i] = thumbColor.getColorForState(states[i], 0);
        i++;

        states[i] = ThemeUtils.CHECKED_STATE_SET;
        colors[i] = getThemeAttrColor(context, R.attr.colorControlActivated);
        i++;

        // Default enabled state
        states[i] = ThemeUtils.EMPTY_STATE_SET;
        colors[i] = thumbColor.getDefaultColor();
        i++;
    } else {
        // Else we'll use an approximation using the default disabled alpha

        // Disabled state
        states[i] = ThemeUtils.DISABLED_STATE_SET;
        colors[i] = getDisabledThemeAttrColor(context, R.attr.colorSwitchThumbNormal);
        i++;

        states[i] = ThemeUtils.CHECKED_STATE_SET;
        colors[i] = getThemeAttrColor(context, R.attr.colorControlActivated);
        i++;

        // Default enabled state
        states[i] = ThemeUtils.EMPTY_STATE_SET;
        colors[i] = getThemeAttrColor(context, R.attr.colorSwitchThumbNormal);
        i++;
    }

    return new ColorStateList(states, colors);
}

From source file:android.support.v7ox.widget.AppCompatDrawableManager.java

private ColorStateList createSwitchThumbColorStateList(Context context) {
    final int[][] states = new int[3][];
    final int[] colors = new int[3];
    int i = 0;//from  w  ww  . j  a  v  a 2  s.  com

    final ColorStateList thumbColor = ThemeUtils.getThemeAttrColorStateList(context,
            R.attr.colorSwitchThumbNormal_ox);

    if (thumbColor != null && thumbColor.isStateful()) {
        // If colorSwitchThumbNormal is a valid ColorStateList, extract the default and
        // disabled colors from it

        // Disabled state
        states[i] = ThemeUtils.DISABLED_STATE_SET;
        colors[i] = thumbColor.getColorForState(states[i], 0);
        i++;

        states[i] = ThemeUtils.CHECKED_STATE_SET;
        colors[i] = ThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated_ox);
        i++;

        // Default enabled state
        states[i] = ThemeUtils.EMPTY_STATE_SET;
        colors[i] = thumbColor.getDefaultColor();
        i++;
    } else {
        // Else we'll use an approximation using the default disabled alpha

        // Disabled state
        states[i] = ThemeUtils.DISABLED_STATE_SET;
        colors[i] = ThemeUtils.getDisabledThemeAttrColor(context, R.attr.colorSwitchThumbNormal_ox);
        i++;

        states[i] = ThemeUtils.CHECKED_STATE_SET;
        colors[i] = ThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated_ox);
        i++;

        // Default enabled state
        states[i] = ThemeUtils.EMPTY_STATE_SET;
        colors[i] = ThemeUtils.getThemeAttrColor(context, R.attr.colorSwitchThumbNormal_ox);
        i++;
    }

    return new ColorStateList(states, colors);
}