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

The following examples show how to use android.content.res.ColorStateList#getDefaultColor() . 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: NumberPadTimePickerBottomSheetComponent.java    From NumberPadTimePicker with Apache License 2.0 6 votes vote down vote up
@NonNull
private static int[] extractColors(ColorStateList colorStateList, int[][] states) {
    int[] colors = new int[states.length];
    int idx = 0;
    for (int[] stateSet : states) {
        // The empty state is peculiar in that getColorForState() will not return
        // the default color, but rather any color defined in the ColorStateList
        // for any state.
        // https://developer.android.com/reference/android/content/res/ColorStateList.html
        // "Each item defines a set of state spec and color pairs, where the state
        // spec is a series of attributes set to either true or false to represent
        // inclusion or exclusion. If an attribute is not specified for an item,
        // it may be any value."
        // "An item with no state spec is considered to match any set of states
        // and is generally useful as a final item to be used as a default."
        colors[idx++] = stateSet.length == 0 ? colorStateList.getDefaultColor()
                : colorStateList.getColorForState(stateSet, 0);
    }
    return colors;
}
 
Example 2
Source File: SkinMaterialNavigationView.java    From Android-skin-support with MIT License 6 votes vote down vote up
private ColorStateList createDefaultColorStateList(int baseColorThemeAttr) {
    final TypedValue value = new TypedValue();
    if (!getContext().getTheme().resolveAttribute(baseColorThemeAttr, value, true)) {
        return null;
    }
    ColorStateList baseColor = SkinCompatResources.getColorStateList(getContext(), value.resourceId);

    int colorPrimary = SkinCompatResources.getColor(getContext(), mDefaultTintResId);
    int defaultColor = baseColor.getDefaultColor();
    return new ColorStateList(new int[][]{
            DISABLED_STATE_SET,
            CHECKED_STATE_SET,
            EMPTY_STATE_SET
    }, new int[]{
            baseColor.getColorForState(DISABLED_STATE_SET, defaultColor),
            colorPrimary,
            defaultColor
    });
}
 
Example 3
Source File: GeneralDelegate.java    From Android-SpeedyViewSelector with Apache License 2.0 6 votes vote down vote up
void applyGeneralAttributeSet(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    ColorStateList textColors = mGeneralSelector.getTextColors();
    if(textColors != null){
        mDefaultColor = textColors.getDefaultColor();
    }
    mSpeedyColorStateList = new SpeedyColorStateList(mDefaultColor);
    if(attrs != null){
        TypedArray a = mGeneralSelector.getContext().obtainStyledAttributes(attrs, R.styleable.SpeedySelectorTextColor, defStyleAttr, defStyleRes);
        if(a != null){
            parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateDefaultTextColor, StateType.STATE_DEFAULT);
            parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateFocusedTextColor, StateType.STATE_FOCUSED);
            parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateWindowFocusedTextColor, StateType.STATE_WINDOW_FOCUSED);
            parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateEnabledTextColor, StateType.STATE_ENABLED);
            parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateCheckedTextColor, StateType.STATE_CHECKED);
            parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateSelectedTextColor, StateType.STATE_SELECTED);
            parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateActivatedTextColor, StateType.STATE_ACTIVATED);
            parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStatePressedTextColor, StateType.STATE_PRESSED);
            parserGeneralAttribute(a,R.styleable.SpeedySelectorTextColor_spStateActiveTextColor,StateType.STATE_ACTIVE);
            a.recycle();
        }
    }
    requestSelector();
}
 
Example 4
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 5
Source File: AlmostRippleDrawable.java    From discreteSeekBar with Apache License 2.0 5 votes vote down vote up
public void setColor(@NonNull ColorStateList tintStateList) {
    int defaultColor = tintStateList.getDefaultColor();
    mFocusedColor = tintStateList.getColorForState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, defaultColor);
    mPressedColor = tintStateList.getColorForState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, defaultColor);
    mDisabledColor = tintStateList.getColorForState(new int[]{-android.R.attr.state_enabled}, defaultColor);

    //The ripple should be partially transparent
    mFocusedColor = getModulatedAlphaColor(130, mFocusedColor);
    mPressedColor = getModulatedAlphaColor(130, mPressedColor);
    mDisabledColor = getModulatedAlphaColor(130, mDisabledColor);
}
 
Example 6
Source File: MarkerDrawable.java    From FuAgoraDemoDroid with MIT License 5 votes vote down vote up
public MarkerDrawable(@NonNull ColorStateList tintList, int closedSize) {
    super(tintList);
    mInterpolator = new AccelerateDecelerateInterpolator();
    mClosedStateSize = closedSize;
    mStartColor = tintList.getColorForState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, tintList.getDefaultColor());
    mEndColor = tintList.getDefaultColor();

}
 
Example 7
Source File: MarkerDrawable.java    From IdealMedia with Apache License 2.0 5 votes vote down vote up
public MarkerDrawable(@NonNull ColorStateList tintList, int closedSize) {
    super(tintList);
    mInterpolator = new AccelerateDecelerateInterpolator();
    mClosedStateSize = closedSize;
    mStartColor = tintList.getColorForState(new int[]{android.R.attr.state_pressed}, tintList.getDefaultColor());
    mEndColor = tintList.getDefaultColor();

}
 
Example 8
Source File: MarkerDrawable.java    From Musicoco with Apache License 2.0 5 votes vote down vote up
public MarkerDrawable(@NonNull ColorStateList tintList, int closedSize) {
    super(tintList);
    mInterpolator = new AccelerateDecelerateInterpolator();
    mClosedStateSize = closedSize;
    mStartColor = tintList.getColorForState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, tintList.getDefaultColor());
    mEndColor = tintList.getDefaultColor();

}
 
Example 9
Source File: AlmostRippleDrawable.java    From FuAgoraDemoDroid with MIT License 5 votes vote down vote up
public void setColor(@NonNull ColorStateList tintStateList) {
    int defaultColor = tintStateList.getDefaultColor();
    mFocusedColor = tintStateList.getColorForState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, defaultColor);
    mPressedColor = tintStateList.getColorForState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, defaultColor);
    mDisabledColor = tintStateList.getColorForState(new int[]{-android.R.attr.state_enabled}, defaultColor);

    //The ripple should be partially transparent
    mFocusedColor = getModulatedAlphaColor(130, mFocusedColor);
    mPressedColor = getModulatedAlphaColor(130, mPressedColor);
    mDisabledColor = getModulatedAlphaColor(130, mDisabledColor);
}
 
Example 10
Source File: BannerView.java    From ReadMark with Apache License 2.0 5 votes vote down vote up
/**
 * 设置DOtView的focus时的颜色
 * @param color
 */
public void setBannerDotColor(ColorStateList color){
    mIndicatorFocusDrawable = new ColorDrawable(color.getDefaultColor());
    if(mCurrentDot != null) {
        mCurrentDot.setDrawable(mIndicatorFocusDrawable);
    }
}
 
Example 11
Source File: BaseAlarmViewHolder.java    From ClockPlus with GNU General Public License v3.0 5 votes vote down vote up
private void bindTime(Alarm alarm) {
        String time = DateFormat.getTimeFormat(getContext()).format(new Date(alarm.ringsAt()));
        if (DateFormat.is24HourFormat(getContext())) {
            mTime.setText(time);
        } else {
            TimeTextUtils.setText(time, mTime);
        }

        // Use a mock TextView to get our colors, because its ColorStateList is never
        // mutated for the lifetime of this ViewHolder (even when reused).
        // This solution is robust against dark/light theme changes, whereas using
        // color resources is not.
        TextView colorsSource = (TextView) itemView.findViewById(R.id.colors_source);
        ColorStateList colors = colorsSource.getTextColors();
        int def = colors.getDefaultColor();
        // Too light
//        int disabled = colors.getColorForState(new int[] {-android.R.attr.state_enabled}, def);
        // Material guidelines say text hints and disabled text should have the same color.
        int disabled = colorsSource.getCurrentHintTextColor();
        // However, digging around in the system's textColorHint for 21+ says its 50% black for our
        // light theme. I'd like to follow what the guidelines says, but I want code that is robust
        // against theme changes. Alternatively, override the attribute values to what you want
        // in both your dark and light themes...
//        int disabled = ContextCompat.getColor(getContext(), R.color.text_color_disabled_light);
        // We only have two states, so we don't care about losing the other state colors.
        mTime.setTextColor(alarm.isEnabled() ? def : disabled);
    }
 
Example 12
Source File: BannerView.java    From ReadMark with Apache License 2.0 5 votes vote down vote up
/**
 * 设置DOtView的focus时的颜色
 * @param color
 */
public void setBannerDotColor(ColorStateList color){
    mIndicatorFocusDrawable = new ColorDrawable(color.getDefaultColor());
    if(mCurrentDot != null) {
        mCurrentDot.setDrawable(mIndicatorFocusDrawable);
    }
}
 
Example 13
Source File: DatePickerCalendarDelegate.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
/**
 * The legacy text color might have been poorly defined. Ensures that it
 * has an appropriate activated state, using the selected state if one
 * exists or modifying the default text color otherwise.
 *
 * @param color a legacy text color, or {@code null}
 * @return a color state list with an appropriate activated state, or
 * {@code null} if a valid activated state could not be generated
 */
@Nullable
private ColorStateList applyLegacyColorFixes(@Nullable ColorStateList color) {
    if (color == null || Utils.colorHasState(color, android.R.attr.state_activated)) { // color.hasState(android.R.attr.state_activated)
        return color;
    }

    final int activatedColor;
    final int defaultColor;

    if (Utils.colorHasState(color, android.R.attr.state_selected)) { // color.hasState(android.R.attr.state_selected)
        activatedColor = color.getColorForState(StateSet.get(
                StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_SELECTED), 0);
        defaultColor = color.getColorForState(StateSet.get(
                StateSet.VIEW_STATE_ENABLED), 0);
    } else {
        activatedColor = color.getDefaultColor();

        // Generate a non-activated color using the disabled alpha.
        final TypedArray ta = mContext.obtainStyledAttributes(ATTRS_DISABLED_ALPHA);
        final float disabledAlpha = ta.getFloat(0, 0.30f);
        defaultColor = multiplyAlphaComponent(activatedColor, disabledAlpha);
        ta.recycle();
    }

    if (activatedColor == 0 || defaultColor == 0) {
        // We somehow failed to obtain the colors.
        return null;
    }

    final int[][] stateSet = new int[][]{{android.R.attr.state_activated}, {}};
    final int[] colors = new int[]{activatedColor, defaultColor};
    return new ColorStateList(stateSet, colors);
}
 
Example 14
Source File: MarkerDrawable.java    From sealrtc-android with MIT License 5 votes vote down vote up
public MarkerDrawable(@NonNull ColorStateList tintList, int closedSize) {
    super(tintList);
    mInterpolator = new AccelerateDecelerateInterpolator();
    mClosedStateSize = closedSize;
    mStartColor =
            tintList.getColorForState(
                    new int[] {android.R.attr.state_enabled, android.R.attr.state_pressed},
                    tintList.getDefaultColor());
    mEndColor = tintList.getDefaultColor();
}
 
Example 15
Source File: MarkerDrawable.java    From Panoramic-Screenshot with GNU General Public License v3.0 5 votes vote down vote up
public MarkerDrawable(@NonNull ColorStateList tintList, int closedSize) {
    super(tintList);
    mInterpolator = new AccelerateDecelerateInterpolator();
    mClosedStateSize = closedSize;
    mStartColor = tintList.getColorForState(new int[]{android.R.attr.state_pressed}, tintList.getDefaultColor());
    mEndColor = tintList.getDefaultColor();

}
 
Example 16
Source File: DatePickerCalendarDelegate.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * The legacy text color might have been poorly defined. Ensures that it
 * has an appropriate activated state, using the selected state if one
 * exists or modifying the default text color otherwise.
 *
 * @param color a legacy text color, or {@code null}
 * @return a color state list with an appropriate activated state, or
 *         {@code null} if a valid activated state could not be generated
 */
@Nullable
private ColorStateList applyLegacyColorFixes(@Nullable ColorStateList color) {
    if (color == null || color.hasState(R.attr.state_activated)) {
        return color;
    }

    final int activatedColor;
    final int defaultColor;
    if (color.hasState(R.attr.state_selected)) {
        activatedColor = color.getColorForState(StateSet.get(
                StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_SELECTED), 0);
        defaultColor = color.getColorForState(StateSet.get(
                StateSet.VIEW_STATE_ENABLED), 0);
    } else {
        activatedColor = color.getDefaultColor();

        // Generate a non-activated color using the disabled alpha.
        final TypedArray ta = mContext.obtainStyledAttributes(ATTRS_DISABLED_ALPHA);
        final float disabledAlpha = ta.getFloat(0, 0.30f);
        defaultColor = multiplyAlphaComponent(activatedColor, disabledAlpha);
    }

    if (activatedColor == 0 || defaultColor == 0) {
        // We somehow failed to obtain the colors.
        return null;
    }

    final int[][] stateSet = new int[][] {{ R.attr.state_activated }, {}};
    final int[] colors = new int[] { activatedColor, defaultColor };
    return new ColorStateList(stateSet, colors);
}
 
Example 17
Source File: BubbleTextView.java    From LaunchEnr with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void setTextColor(ColorStateList colors) {
    mTextColor = colors.getDefaultColor();
    super.setTextColor(colors);
}
 
Example 18
Source File: BubbleTextView.java    From Trebuchet with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void setTextColor(ColorStateList colors) {
    mTextColor = colors.getDefaultColor();
    super.setTextColor(colors);
}
 
Example 19
Source File: StateDrawable.java    From android-open-project-demo with Apache License 2.0 4 votes vote down vote up
public StateDrawable(@NonNull ColorStateList tintStateList) {
    super();
    mTintStateList = tintStateList;
    mCurrentColor = tintStateList.getDefaultColor();
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
 
Example 20
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);
}