Java Code Examples for android.content.res.ColorStateList#isStateful()

The following examples show how to use android.content.res.ColorStateList#isStateful() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextInputLayout.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * Set the box's stroke color state list.
 *
 * @param boxStrokeColorStateList the color state list to use for the box's stroke
 */
public void setBoxStrokeColorStateList(@NonNull ColorStateList boxStrokeColorStateList) {
  if (boxStrokeColorStateList.isStateful()) {
    defaultStrokeColor = boxStrokeColorStateList.getDefaultColor();
    disabledColor =
        boxStrokeColorStateList.getColorForState(new int[] {-android.R.attr.state_enabled}, -1);
    hoveredStrokeColor =
        boxStrokeColorStateList.getColorForState(
            new int[] {android.R.attr.state_hovered, android.R.attr.state_enabled}, -1);
    focusedStrokeColor =
        boxStrokeColorStateList.getColorForState(
            new int[] {android.R.attr.state_focused, android.R.attr.state_enabled}, -1);
  } else if (focusedStrokeColor != boxStrokeColorStateList.getDefaultColor()) {
    // If attribute boxStrokeColor is not a color state list but only a single value, its value
    // will be applied to the box's focus state.
    focusedStrokeColor = boxStrokeColorStateList.getDefaultColor();
  }
  updateTextInputBoxState();
}
 
Example 2
Source File: Tints.java    From AndroidCommons with Apache License 2.0 6 votes vote down vote up
public static Tint withAlpha(@NonNull final Tint tint, final int alpha) {
    return new Tint() {
        @Override
        public ColorStateList getColor(Context context) {
            int appliedAlpha = alpha == ALPHA_DISABLED ? getThemeDisabledAlpha(context) : alpha;
            ColorStateList stateColor = tint.getColor(context);

            if (stateColor.isStateful()) {
                // New ColorStateList object will be created each time calling .withAlpha()
                return stateColor.withAlpha(appliedAlpha);
            } else {
                // Created ColorStateList object will be cached
                int color = (stateColor.getDefaultColor() & 0xFFFFFF) | (appliedAlpha << 24);
                return ColorStateList.valueOf(color);
            }
        }
    };
}
 
Example 3
Source File: ThemeUtils.java    From timecat with Apache License 2.0 5 votes vote down vote up
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;
        }

        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()));
}
 
Example 4
Source File: RoundButton.java    From roundbutton with Apache License 2.0 5 votes vote down vote up
public RoundButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);


    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundButton);

    float pressedRatio = a.getFloat(R.styleable.RoundButton_btnPressedRatio, 0.80f);
    int cornerRadius = a.getLayoutDimension(R.styleable.RoundButton_btnCornerRadius, 0);

    ColorStateList solidColor = a.getColorStateList(R.styleable.RoundButton_btnSolidColor);
    int strokeColor = a.getColor(R.styleable.RoundButton_btnStrokeColor, 0x0);
    int strokeWidth = a.getDimensionPixelSize(R.styleable.RoundButton_btnStrokeWidth, 0);
    int strokeDashWidth = a.getDimensionPixelSize(R.styleable.RoundButton_btnStrokeDashWidth, 0);
    int strokeDashGap = a.getDimensionPixelSize(R.styleable.RoundButton_btnStrokeDashGap, 0);

    a.recycle();

    setSingleLine(true);
    setGravity(Gravity.CENTER);

    RoundDrawable rd = new RoundDrawable(cornerRadius == -1);
    rd.setCornerRadius(cornerRadius == -1 ? 0 : cornerRadius);
    rd.setStroke(strokeWidth, strokeColor, strokeDashWidth, strokeDashGap);

    if (solidColor == null) {
        solidColor = ColorStateList.valueOf(0);
    }
    if (solidColor.isStateful()) {
        rd.setSolidColors(solidColor);
    } else if (pressedRatio > 0.0001f) {
        rd.setSolidColors(csl(solidColor.getDefaultColor(), pressedRatio));
    } else {
        rd.setColor(solidColor.getDefaultColor());
    }
    setBackground(rd);
}
 
Example 5
Source File: ThemeUtils.java    From MagicaSakura with Apache License 2.0 5 votes vote down vote up
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;
        }

        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()));
}
 
Example 6
Source File: TextInputLayout.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void refreshIconDrawableState(
    CheckableImageButton iconView, ColorStateList colorStateList) {
  Drawable icon = iconView.getDrawable();
  if (iconView.getDrawable() == null || colorStateList == null || !colorStateList.isStateful()) {
    return;
  }

  int color =
      colorStateList.getColorForState(mergeIconState(iconView), colorStateList.getDefaultColor());

  icon = DrawableCompat.wrap(icon).mutate();
  DrawableCompat.setTintList(icon, ColorStateList.valueOf(color));
  iconView.setImageDrawable(icon);
}
 
Example 7
Source File: CreditsRollView.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void setTextColor(ColorStateList colors) {
    super.setTextColor(colors);

    if (!colors.isStateful()) {
        mTextColor = colors.getDefaultColor();
    }
    else {
        mTextColor = colors.getColorForState(getDrawableState(), colors.getDefaultColor());
    }

    initTextPaint();
    invalidate();
}
 
Example 8
Source File: SkinCompatDrawableManager.java    From Android-skin-support with MIT License 4 votes vote down vote up
private ColorStateList createSwitchThumbColorStateList(Context context) {
    final int[][] states = new int[3][];
    final int[] colors = new int[3];
    int i = 0;

    final ColorStateList thumbColor = SkinCompatThemeUtils.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] = SkinCompatThemeUtils.DISABLED_STATE_SET;
        colors[i] = thumbColor.getColorForState(states[i], 0);
        i++;

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

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

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

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

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

    return new ColorStateList(states, colors);
}
 
Example 9
Source File: SkinCompatDrawableManager.java    From Android-skin-support with MIT License 4 votes vote down vote up
private ColorStateList createSwitchThumbColorStateList(Context context) {
    final int[][] states = new int[3][];
    final int[] colors = new int[3];
    int i = 0;

    final ColorStateList thumbColor = SkinCompatThemeUtils.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] = SkinCompatThemeUtils.DISABLED_STATE_SET;
        colors[i] = thumbColor.getColorForState(states[i], 0);
        i++;

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

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

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

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

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

    return new ColorStateList(states, colors);
}
 
Example 10
Source File: ChipDrawable.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private static boolean isStateful(@Nullable ColorStateList colorStateList) {
  return colorStateList != null && colorStateList.isStateful();
}
 
Example 11
Source File: TintAwareDrawable.java    From ProjectX with Apache License 2.0 4 votes vote down vote up
public boolean isStateful() {
    ColorStateList tintList = this.isCompatTintEnabled() && this.mState != null ? this.mState.mTint : null;
    return tintList != null && tintList.isStateful() || this.mDrawable.isStateful();
}