Example usage for android.content.res ColorStateList getColorForState

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

Introduction

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

Prototype

public int getColorForState(@Nullable int[] stateSet, int defaultColor) 

Source Link

Document

Return the color associated with the given set of android.view.View states.

Usage

From source file:android.support.design.widget.CircularBorderDrawable.java

void setBorderTint(ColorStateList tint) {
    if (tint != null) {
        mCurrentBorderTintColor = tint.getColorForState(getState(), mCurrentBorderTintColor);
    }/*from w  w  w  . j  a va2  s  .  com*/
    mBorderTint = tint;
    mInvalidateShader = true;
    invalidateSelf();
}

From source file:org.adw.library.widgets.discreteseekbar.internal.drawable.AlmostRippleDrawable.java

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);
}

From source file:org.adw.library.widgets.discreteseekbar.internal.drawable.MarkerDrawable.java

public MarkerDrawable(@NonNull ColorStateList tintList, int closedSize) {
    super(tintList);
    mInterpolator = new FastOutSlowInInterpolator();
    mClosedStateSize = closedSize;/*from   w ww  .ja  va  2 s  .  c  o m*/
    mStartColor = tintList.getColorForState(
            new int[] { android.R.attr.state_enabled, android.R.attr.state_pressed },
            tintList.getDefaultColor());
    mEndColor = tintList.getDefaultColor();

}

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

public void setTintButtonDrawable(ColorStateList colorStateList) {
    mIsAnimationButton = true;//from  w w  w.j  av a  2  s  .co m
    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:com.yy.androidlib.widget.tab.PagerSlidingTabStrip.java

public void setTextColorResource(int resId) {
    ColorStateList colorStateList = getResources().getColorStateList(resId);
    this.tabTextColor = colorStateList.getDefaultColor();
    this.tabTextCheckedColor = colorStateList.getColorForState(new int[] { android.R.attr.state_checked },
            tabTextColor);//from  w  w w  .java2s . c  o  m
    updateTabStyles();
}

From source file:io.doist.datetimepicker.date.SimpleMonthView.java

void setTextColor(ColorStateList colors) {
    final Resources res = getContext().getResources();

    mNormalTextColor = colors.getColorForState(ENABLED_STATE_SET,
            res.getColor(R.color.datepicker_default_normal_text_color_holo_light));
    mMonthTitlePaint.setColor(mNormalTextColor);
    mMonthDayLabelPaint.setColor(mNormalTextColor);

    mDisabledTextColor = colors.getColorForState(EMPTY_STATE_SET,
            res.getColor(R.color.datepicker_default_disabled_text_color_holo_light));
    mDayNumberDisabledPaint.setColor(mDisabledTextColor);

    mSelectedDayColor = colors.getColorForState(ENABLED_SELECTED_STATE_SET,
            res.getColor(android.R.color.holo_blue_light));
    mDayNumberSelectedPaint.setColor(mSelectedDayColor);
    mDayNumberSelectedPaint.setAlpha(SELECTED_CIRCLE_ALPHA);
}

From source file:com.appeaser.sublimenavigationviewlibrary.SublimeThemer.java

private void setDefaultIconTintList() {
    TypedValue value = new TypedValue();
    if (mContext.getTheme().resolveAttribute(android.R.attr.textColorPrimary, value, true)) {
        ColorStateList baseColor = mContext.getResources().getColorStateList(value.resourceId);
        if (mContext.getTheme().resolveAttribute(R.attr.colorPrimary, value, true)) {
            int colorPrimary = value.data;
            int defaultColor = baseColor.getDefaultColor();
            mIconTintList = new ColorStateList(
                    new int[][] { DISABLED_STATE_SET, CHECKED_STATE_SET, EMPTY_STATE_SET },
                    new int[] { baseColor.getColorForState(DISABLED_STATE_SET, defaultColor), colorPrimary,
                            defaultColor });
        }/*from  w  w  w.j a v a  2 s .c om*/
    }

    if (mIconTintList == null) {
        // Defaults
        boolean isLightTheme = mDefaultTheme == SublimeThemer.DefaultTheme.LIGHT;

        int defDisabled = isLightTheme
                ? mContext.getResources().getColor(R.color.snv_primary_text_disabled_material_light)
                : mContext.getResources().getColor(R.color.snv_primary_text_disabled_material_dark);
        int defChecked = isLightTheme ? mContext.getResources().getColor(R.color.snv_primary_material_light)
                : mContext.getResources().getColor(R.color.snv_primary_material_dark);
        int defEmptySet = isLightTheme
                ? mContext.getResources().getColor(R.color.snv_primary_text_default_material_light)
                : mContext.getResources().getColor(R.color.snv_primary_text_default_material_dark);
        mIconTintList = new ColorStateList(
                new int[][] { DISABLED_STATE_SET, CHECKED_STATE_SET, EMPTY_STATE_SET },
                new int[] { defDisabled, defChecked, defEmptySet });
    }
}

From source file:com.example.administrator.smartbj.Drawer.MyNavigationView.java

private ColorStateList createDefaultColorStateList(int baseColorThemeAttr) {
    TypedValue value = new TypedValue();
    if (!getContext().getTheme().resolveAttribute(baseColorThemeAttr, value, true)) {
        return null;
    }// w w w.ja  v  a 2  s . c  o m
    ColorStateList baseColor = getResources().getColorStateList(value.resourceId);
    if (!getContext().getTheme().resolveAttribute(android.support.design.R.attr.colorPrimary, value, true)) {
        return null;
    }
    int colorPrimary = value.data;
    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 });
}

From source file:com.appeaser.sublimenavigationviewlibrary.SublimeThemer.java

private void setDefaultCheckableItemTintList() {
    TypedValue value = new TypedValue();
    if (mContext.getTheme().resolveAttribute(android.R.attr.textColorPrimary, value, true)) {
        ColorStateList baseColor = mContext.getResources().getColorStateList(value.resourceId);
        if (mContext.getTheme().resolveAttribute(R.attr.colorPrimary, value, true)) {
            int colorPrimary = value.data;
            int defaultColor = baseColor.getDefaultColor();
            mCheckableItemTintList = new ColorStateList(
                    new int[][] { DISABLED_STATE_SET, CHECKABLE_ITEM_CHECKED_STATE_SET, EMPTY_STATE_SET },
                    new int[] { baseColor.getColorForState(DISABLED_STATE_SET, defaultColor), colorPrimary,
                            defaultColor });
        }/*w ww.  j  ava2 s  . c  om*/
    }

    if (mCheckableItemTintList == null) {
        // Defaults
        boolean isLightTheme = mDefaultTheme == SublimeThemer.DefaultTheme.LIGHT;

        int defDisabled = isLightTheme
                ? mContext.getResources().getColor(R.color.snv_primary_text_disabled_material_light)
                : mContext.getResources().getColor(R.color.snv_primary_text_disabled_material_dark);
        int defChecked = isLightTheme ? mContext.getResources().getColor(R.color.snv_primary_material_light)
                : mContext.getResources().getColor(R.color.snv_primary_material_dark);
        int defEmptySet = isLightTheme
                ? mContext.getResources().getColor(R.color.snv_primary_text_default_material_light)
                : mContext.getResources().getColor(R.color.snv_primary_text_default_material_dark);
        mCheckableItemTintList = new ColorStateList(
                new int[][] { DISABLED_STATE_SET, CHECKABLE_ITEM_CHECKED_STATE_SET, EMPTY_STATE_SET },
                new int[] { defDisabled, defChecked, defEmptySet });
    }
}

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  w ww. jav  a 2s  . 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);
}