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

The following examples show how to use android.content.res.ColorStateList#valueOf() . 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: EmTintUtils.java    From AndroidTint with Apache License 2.0 6 votes vote down vote up
public static void setTint(@NonNull Button button, @ColorInt int color) {
    ColorStateList s1 = ColorStateList.valueOf(color);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        button.setCompoundDrawableTintList(s1);
    } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
        Drawable drawable = DrawableCompat.wrap(button.getCompoundDrawables()[1]);
        button.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
        DrawableCompat.setTintList(drawable, s1);
    } else {
        PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
            mode = PorterDuff.Mode.MULTIPLY;
        }
        if (button.getCompoundDrawables()[1] != null)
            button.getCompoundDrawables()[1].setColorFilter(color, mode);
    }
}
 
Example 2
Source File: ProgressView.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
private void setTint(@ColorInt int color,
                            boolean skipIndeterminate) {
    ColorStateList sl = ColorStateList.valueOf(color);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        progress.setProgressTintList(sl);
        progress.setSecondaryProgressTintList(sl);
        if (!skipIndeterminate)
            progress.setIndeterminateTintList(sl);
    } else {
        PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
            mode = PorterDuff.Mode.MULTIPLY;
        }
        if (!skipIndeterminate && progress.getIndeterminateDrawable() != null)
            progress.getIndeterminateDrawable().setColorFilter(color, mode);
        if (progress.getProgressDrawable() != null)
            progress.getProgressDrawable().setColorFilter(color, mode);
    }
}
 
Example 3
Source File: RippleUtils.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link ColorStateList} that is safe to pass to {@link
 * android.graphics.drawable.RippleDrawable}.
 *
 * <p>If given a null ColorStateList, this will return a new transparent ColorStateList since
 * RippleDrawable requires a non-null ColorStateList.
 *
 * <p>If given a non-null ColorStateList, this method will log a warning for API 22-27 if the
 * ColorStateList is transparent in the default state and non-transparent in the pressed state.
 * This will result in using the pressed state color for the ripple until the finger is lifted at
 * which point the ripple will transition to the default state color (transparent), making the
 * ripple appear to terminate prematurely.
 */
@NonNull
public static ColorStateList sanitizeRippleDrawableColor(@Nullable ColorStateList rippleColor) {
  if (rippleColor != null) {
    if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP_MR1
        && VERSION.SDK_INT <= VERSION_CODES.O_MR1
        && Color.alpha(rippleColor.getDefaultColor()) == 0
        && Color.alpha(rippleColor.getColorForState(ENABLED_PRESSED_STATE_SET, Color.TRANSPARENT))
            != 0) {
      Log.w(LOG_TAG, TRANSPARENT_DEFAULT_COLOR_WARNING);
    }
    return rippleColor;
  }
  return ColorStateList.valueOf(Color.TRANSPARENT);
}
 
Example 4
Source File: CircleProgressbar.java    From YCProgress with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化。
 *
 * @param context      上下文。
 * @param attributeSet 属性。
 */
private void initialize(Context context, AttributeSet attributeSet) {
    mPaint.setAntiAlias(true);
    TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CircleProgressbar);
    if (typedArray.hasValue(R.styleable.CircleProgressbar_circle_color)){
        inCircleColors = typedArray.getColorStateList(R.styleable.CircleProgressbar_circle_color);
    } else{
        inCircleColors = ColorStateList.valueOf(Color.TRANSPARENT);
    }
    circleColor = inCircleColors.getColorForState(getDrawableState(), Color.TRANSPARENT);
    typedArray.recycle();
}
 
Example 5
Source File: SUtils.java    From SublimePicker with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Drawable createButtonRippleBg(Context context,
                                             int colorButtonNormal,
                                             int colorControlHighlight) {
    return new RippleDrawable(ColorStateList.valueOf(colorControlHighlight),
            null, createButtonShape(context, colorButtonNormal));
}
 
Example 6
Source File: RoundedImageView.java    From ClipCircleHeadLikeQQ with Apache License 2.0 5 votes vote down vote up
public void setBorderColor(ColorStateList colors) {
  if (mBorderColor.equals(colors)) { return; }

  mBorderColor =
      (colors != null) ? colors : ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
  updateDrawableAttrs();
  updateBackgroundDrawableAttrs(false);
  if (mBorderWidth > 0) {
    invalidate();
  }
}
 
Example 7
Source File: ActiveItemImageView.java    From letv with Apache License 2.0 5 votes vote down vote up
public ActiveItemImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.cornerRadius = 0.0f;
    this.borderWidth = 0.0f;
    this.borderColor = ColorStateList.valueOf(-16777216);
    this.isOval = false;
    this.mutateBackground = false;
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView, defStyle, 0);
    int index = a.getInt(R.styleable.RoundedImageView_android_scaleType, -1);
    if (index >= 0) {
        setScaleType(SCALE_TYPES[index]);
    } else {
        setScaleType(ScaleType.FIT_CENTER);
    }
    this.cornerRadius = (float) a.getDimensionPixelSize(R.styleable.RoundedImageView_riv_corner_radius, -1);
    this.borderWidth = (float) a.getDimensionPixelSize(R.styleable.RoundedImageView_riv_border_width, -1);
    if (this.cornerRadius < 0.0f) {
        this.cornerRadius = 0.0f;
    }
    if (this.borderWidth < 0.0f) {
        this.borderWidth = 0.0f;
    }
    this.borderColor = a.getColorStateList(R.styleable.RoundedImageView_riv_border_color);
    if (this.borderColor == null) {
        this.borderColor = ColorStateList.valueOf(-16777216);
    }
    this.mutateBackground = a.getBoolean(R.styleable.RoundedImageView_riv_mutate_background, false);
    this.isOval = a.getBoolean(R.styleable.RoundedImageView_riv_oval, false);
    updateDrawableAttrs();
    updateBackgroundDrawableAttrs(true);
    a.recycle();
}
 
Example 8
Source File: RoundedImageView.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public void setBorderColor(ColorStateList colors) {
  if (borderColor.equals(colors)) { return; }

  borderColor =
      (colors != null) ? colors : ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
  updateDrawableAttrs();
  updateBackgroundDrawableAttrs(false);
  if (borderWidth > 0) {
    invalidate();
  }
}
 
Example 9
Source File: Theme.java    From APlayer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 按下触摸效果
 */
public static Drawable getPressDrawable(Drawable defaultDrawable, Drawable effectDrawable,
    @ColorInt int rippleColor, Drawable contentDrawable, Drawable maskDrawable) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    return new RippleDrawable(ColorStateList.valueOf(rippleColor),
        contentDrawable,
        maskDrawable);
  } else {
    StateListDrawable stateListDrawable = new StateListDrawable();
    stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, effectDrawable);
    stateListDrawable.addState(new int[]{}, defaultDrawable);
    return stateListDrawable;
  }
}
 
Example 10
Source File: CoordinatorLayout.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@Override
public void setElevationShadowColor(int color) {
    ambientShadowColor = spotShadowColor = ColorStateList.valueOf(color);
    setElevation(elevation);
    setTranslationZ(translationZ);
}
 
Example 11
Source File: TitleBarView.java    From UIWidget with Apache License 2.0 4 votes vote down vote up
public TitleBarView setActionTextColor(int mActionTextColor) {
    this.mActionTextColor = ColorStateList.valueOf(mActionTextColor);
    return this;
}
 
Example 12
Source File: ImageView.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@Override
public void setElevationShadowColor(int color) {
    ambientShadowColor = spotShadowColor = ColorStateList.valueOf(color);
    setElevation(elevation);
    setTranslationZ(translationZ);
}
 
Example 13
Source File: SmartTabLayout.java    From SmartTabLayout with Apache License 2.0 4 votes vote down vote up
public SmartTabLayout(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);

  // Disable the Scroll Bar
  setHorizontalScrollBarEnabled(false);

  final DisplayMetrics dm = getResources().getDisplayMetrics();
  final float density = dm.density;

  int tabBackgroundResId = NO_ID;
  boolean textAllCaps = TAB_VIEW_TEXT_ALL_CAPS;
  ColorStateList textColors;
  float textSize = TypedValue.applyDimension(
      TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP, dm);
  int textHorizontalPadding = (int) (TAB_VIEW_PADDING_DIPS * density);
  int textMinWidth = (int) (TAB_VIEW_TEXT_MIN_WIDTH * density);
  boolean distributeEvenly = DEFAULT_DISTRIBUTE_EVENLY;
  int customTabLayoutId = NO_ID;
  int customTabTextViewId = NO_ID;
  boolean clickable = TAB_CLICKABLE;
  int titleOffset = (int) (TITLE_OFFSET_DIPS * density);

  TypedArray a = context.obtainStyledAttributes(
      attrs, R.styleable.stl_SmartTabLayout, defStyle, 0);
  tabBackgroundResId = a.getResourceId(
      R.styleable.stl_SmartTabLayout_stl_defaultTabBackground, tabBackgroundResId);
  textAllCaps = a.getBoolean(
      R.styleable.stl_SmartTabLayout_stl_defaultTabTextAllCaps, textAllCaps);
  textColors = a.getColorStateList(
      R.styleable.stl_SmartTabLayout_stl_defaultTabTextColor);
  textSize = a.getDimension(
      R.styleable.stl_SmartTabLayout_stl_defaultTabTextSize, textSize);
  textHorizontalPadding = a.getDimensionPixelSize(
      R.styleable.stl_SmartTabLayout_stl_defaultTabTextHorizontalPadding, textHorizontalPadding);
  textMinWidth = a.getDimensionPixelSize(
      R.styleable.stl_SmartTabLayout_stl_defaultTabTextMinWidth, textMinWidth);
  customTabLayoutId = a.getResourceId(
      R.styleable.stl_SmartTabLayout_stl_customTabTextLayoutId, customTabLayoutId);
  customTabTextViewId = a.getResourceId(
      R.styleable.stl_SmartTabLayout_stl_customTabTextViewId, customTabTextViewId);
  distributeEvenly = a.getBoolean(
      R.styleable.stl_SmartTabLayout_stl_distributeEvenly, distributeEvenly);
  clickable = a.getBoolean(
      R.styleable.stl_SmartTabLayout_stl_clickable, clickable);
  titleOffset = a.getLayoutDimension(
      R.styleable.stl_SmartTabLayout_stl_titleOffset, titleOffset);
  a.recycle();

  this.titleOffset = titleOffset;
  this.tabViewBackgroundResId = tabBackgroundResId;
  this.tabViewTextAllCaps = textAllCaps;
  this.tabViewTextColors = (textColors != null)
      ? textColors
      : ColorStateList.valueOf(TAB_VIEW_TEXT_COLOR);
  this.tabViewTextSize = textSize;
  this.tabViewTextHorizontalPadding = textHorizontalPadding;
  this.tabViewTextMinWidth = textMinWidth;
  this.internalTabClickListener = clickable ? new InternalTabClickListener() : null;
  this.distributeEvenly = distributeEvenly;

  if (customTabLayoutId != NO_ID) {
    setCustomTabView(customTabLayoutId, customTabTextViewId);
  }

  this.tabStrip = new SmartTabStrip(context, attrs);

  if (distributeEvenly && tabStrip.isIndicatorAlwaysInCenter()) {
    throw new UnsupportedOperationException(
        "'distributeEvenly' and 'indicatorAlwaysInCenter' both use does not support");
  }

  // Make sure that the Tab Strips fills this View
  setFillViewport(!tabStrip.isIndicatorAlwaysInCenter());

  addView(tabStrip, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

}
 
Example 14
Source File: EasyTextView.java    From EasyTextView with Apache License 2.0 4 votes vote down vote up
/**
 * 设置右文案颜色
 */
public EasyTextView textRightColor(int color) {
    this.mRightColor = ColorStateList.valueOf(color);
    return this;
}
 
Example 15
Source File: RoundedImageView.java    From android-project-wo2b with Apache License 2.0 4 votes vote down vote up
public RoundedImageView(Context context, AttributeSet attrs, int defStyle)
{
	super(context, attrs, defStyle);

	TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView, defStyle, 0);

	int index = a.getInt(R.styleable.RoundedImageView_android_scaleType, -1);
	if (index >= 0)
	{
		setScaleType(SCALE_TYPES[index]);
	}
	else
	{
		// default scaletype to FIT_CENTER
		setScaleType(ScaleType.FIT_CENTER);
	}

	cornerRadius = a.getDimensionPixelSize(R.styleable.RoundedImageView_corner_radius, -1);
	borderWidth = a.getDimensionPixelSize(R.styleable.RoundedImageView_border_width, -1);

	// don't allow negative values for radius and border
	if (cornerRadius < 0)
	{
		cornerRadius = DEFAULT_RADIUS;
	}
	if (borderWidth < 0)
	{
		borderWidth = DEFAULT_BORDER_WIDTH;
	}

	borderColor = a.getColorStateList(R.styleable.RoundedImageView_border_color);
	if (borderColor == null)
	{
		borderColor = ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
	}

	mutateBackground = a.getBoolean(R.styleable.RoundedImageView_mutate_background, false);
	isOval = a.getBoolean(R.styleable.RoundedImageView_oval, false);

	updateDrawableAttrs();
	updateBackgroundDrawableAttrs(true);

	a.recycle();
}
 
Example 16
Source File: ClipDrawable.java    From ProjectX with Apache License 2.0 4 votes vote down vote up
public ClipDrawable(Drawable drawable, ClipOutlineProvider provider, float strokeWidth,
                    int strokeColor) {
    this(drawable, provider, strokeWidth, ColorStateList.valueOf(strokeColor));
}
 
Example 17
Source File: ChipView.java    From MaterialChipsInput with Apache License 2.0 4 votes vote down vote up
/**
 * Set delete icon color
 *
 * @param color the color to set
 */
public void setDeleteIconColor(@ColorInt int color) {
    mDeleteIconColor = ColorStateList.valueOf(color);
    mDeletable = true;
    inflateWithAttributes();
}
 
Example 18
Source File: PassCodeView.java    From android-passcodeview with Apache License 2.0 4 votes vote down vote up
/**
 * Set color for the keypad text
 *
 * @param color - Resource id of the color to be set
 */
public void setKeyTextColor(int color) {
  ColorStateList colorStateList = ColorStateList.valueOf(color);
  textPaint.setColor(colorStateList.getColorForState(getDrawableState(), 0));
  invalidate();
}
 
Example 19
Source File: MaterialAutoCompleteTextView.java    From XERUNG with Apache License 2.0 4 votes vote down vote up
/**
 * Same function as {@link #setHintTextColor(int)}. (The built-in one is a final method that can't be overridden, so use this method instead.)
 */
public void setMetHintTextColor(int color) {
  textColorHintStateList = ColorStateList.valueOf(color);
  resetHintTextColor();
}
 
Example 20
Source File: RoundedTransformationBuilder.java    From Android with MIT License 2 votes vote down vote up
/**
 * Set the border color.
 *
 * @param color the color to set.
 * @return the builder for chaining.
 */
public RoundedTransformationBuilder borderColor(int color) {
  mBorderColor = ColorStateList.valueOf(color);
  return this;
}