Search in sources :

Example 86 with ColorStateList

use of android.content.res.ColorStateList in project android_frameworks_base by DirtyUnicorns.

the class TextView method getTextColors.

/**
     * Returns the TextView_textColor attribute from the TypedArray, if set, or
     * the TextAppearance_textColor from the TextView_textAppearance attribute,
     * if TextView_textColor was not set directly.
     *
     * @removed
     */
public static ColorStateList getTextColors(Context context, TypedArray attrs) {
    if (attrs == null) {
        // Preserve behavior prior to removal of this API.
        throw new NullPointerException();
    }
    // It's not safe to use this method from apps. The parameter 'attrs'
    // must have been obtained using the TextView filter array which is not
    // available to the SDK. As such, we grab a default TypedArray with the
    // right filter instead here.
    final TypedArray a = context.obtainStyledAttributes(R.styleable.TextView);
    ColorStateList colors = a.getColorStateList(R.styleable.TextView_textColor);
    if (colors == null) {
        final int ap = a.getResourceId(R.styleable.TextView_textAppearance, 0);
        if (ap != 0) {
            final TypedArray appearance = context.obtainStyledAttributes(ap, R.styleable.TextAppearance);
            colors = appearance.getColorStateList(R.styleable.TextAppearance_textColor);
            appearance.recycle();
        }
    }
    a.recycle();
    return colors;
}
Also used : TypedArray(android.content.res.TypedArray) ColorStateList(android.content.res.ColorStateList) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 87 with ColorStateList

use of android.content.res.ColorStateList in project android_frameworks_base by DirtyUnicorns.

the class TimePickerClockDelegate method applyLegacyColorFixes.

/**
     * 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);
}
Also used : TypedArray(android.content.res.TypedArray) ColorStateList(android.content.res.ColorStateList) Nullable(android.annotation.Nullable)

Example 88 with ColorStateList

use of android.content.res.ColorStateList in project android_frameworks_base by DirtyUnicorns.

the class TextView method setTextAppearance.

/**
     * Sets the text color, size, style, hint color, and highlight color
     * from the specified TextAppearance resource.
     *
     * @deprecated Use {@link #setTextAppearance(int)} instead.
     */
@Deprecated
public void setTextAppearance(Context context, @StyleRes int resId) {
    final TypedArray ta = context.obtainStyledAttributes(resId, R.styleable.TextAppearance);
    final int textColorHighlight = ta.getColor(R.styleable.TextAppearance_textColorHighlight, 0);
    if (textColorHighlight != 0) {
        setHighlightColor(textColorHighlight);
    }
    final ColorStateList textColor = ta.getColorStateList(R.styleable.TextAppearance_textColor);
    if (textColor != null) {
        setTextColor(textColor);
    }
    final int textSize = ta.getDimensionPixelSize(R.styleable.TextAppearance_textSize, 0);
    if (textSize != 0) {
        setRawTextSize(textSize);
    }
    final ColorStateList textColorHint = ta.getColorStateList(R.styleable.TextAppearance_textColorHint);
    if (textColorHint != null) {
        setHintTextColor(textColorHint);
    }
    final ColorStateList textColorLink = ta.getColorStateList(R.styleable.TextAppearance_textColorLink);
    if (textColorLink != null) {
        setLinkTextColor(textColorLink);
    }
    final String fontFamily = ta.getString(R.styleable.TextAppearance_fontFamily);
    final int typefaceIndex = ta.getInt(R.styleable.TextAppearance_typeface, -1);
    final int styleIndex = ta.getInt(R.styleable.TextAppearance_textStyle, -1);
    setTypefaceFromAttrs(fontFamily, typefaceIndex, styleIndex);
    final int shadowColor = ta.getInt(R.styleable.TextAppearance_shadowColor, 0);
    if (shadowColor != 0) {
        final float dx = ta.getFloat(R.styleable.TextAppearance_shadowDx, 0);
        final float dy = ta.getFloat(R.styleable.TextAppearance_shadowDy, 0);
        final float r = ta.getFloat(R.styleable.TextAppearance_shadowRadius, 0);
        setShadowLayer(r, dx, dy, shadowColor);
    }
    if (ta.getBoolean(R.styleable.TextAppearance_textAllCaps, false)) {
        setTransformationMethod(new AllCapsTransformationMethod(getContext()));
    }
    if (ta.hasValue(R.styleable.TextAppearance_elegantTextHeight)) {
        setElegantTextHeight(ta.getBoolean(R.styleable.TextAppearance_elegantTextHeight, false));
    }
    if (ta.hasValue(R.styleable.TextAppearance_letterSpacing)) {
        setLetterSpacing(ta.getFloat(R.styleable.TextAppearance_letterSpacing, 0));
    }
    if (ta.hasValue(R.styleable.TextAppearance_fontFeatureSettings)) {
        setFontFeatureSettings(ta.getString(R.styleable.TextAppearance_fontFeatureSettings));
    }
    ta.recycle();
}
Also used : TypedArray(android.content.res.TypedArray) ColorStateList(android.content.res.ColorStateList) SpannedString(android.text.SpannedString) SpannableString(android.text.SpannableString) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) AllCapsTransformationMethod(android.text.method.AllCapsTransformationMethod)

Example 89 with ColorStateList

use of android.content.res.ColorStateList in project android_frameworks_base by DirtyUnicorns.

the class ShapeDrawable method updateStateFromTypedArray.

private void updateStateFromTypedArray(TypedArray a) {
    final ShapeState state = mShapeState;
    final Paint paint = state.mPaint;
    // Account for any configuration changes.
    state.mChangingConfigurations |= a.getChangingConfigurations();
    // Extract the theme attributes, if any.
    state.mThemeAttrs = a.extractThemeAttrs();
    int color = paint.getColor();
    color = a.getColor(R.styleable.ShapeDrawable_color, color);
    paint.setColor(color);
    boolean dither = paint.isDither();
    dither = a.getBoolean(R.styleable.ShapeDrawable_dither, dither);
    paint.setDither(dither);
    setIntrinsicWidth((int) a.getDimension(R.styleable.ShapeDrawable_width, state.mIntrinsicWidth));
    setIntrinsicHeight((int) a.getDimension(R.styleable.ShapeDrawable_height, state.mIntrinsicHeight));
    final int tintMode = a.getInt(R.styleable.ShapeDrawable_tintMode, -1);
    if (tintMode != -1) {
        state.mTintMode = Drawable.parseTintMode(tintMode, Mode.SRC_IN);
    }
    final ColorStateList tint = a.getColorStateList(R.styleable.ShapeDrawable_tint);
    if (tint != null) {
        state.mTint = tint;
    }
}
Also used : ColorStateList(android.content.res.ColorStateList) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 90 with ColorStateList

use of android.content.res.ColorStateList in project android_frameworks_base by DirtyUnicorns.

the class ColorStateListTest method testStateIsInList.

@SmallTest
public void testStateIsInList() throws Exception {
    ColorStateList colorStateList = mResources.getColorStateList(R.color.color1);
    int[] focusedState = { android.R.attr.state_focused };
    int focusColor = colorStateList.getColorForState(focusedState, R.color.failColor);
    assertEquals(mResources.getColor(R.color.testcolor1), focusColor);
}
Also used : ColorStateList(android.content.res.ColorStateList) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

ColorStateList (android.content.res.ColorStateList)280 Paint (android.graphics.Paint)90 TypedArray (android.content.res.TypedArray)76 Drawable (android.graphics.drawable.Drawable)48 TextPaint (android.text.TextPaint)42 Resources (android.content.res.Resources)25 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)22 TypedValue (android.util.TypedValue)21 TextView (android.widget.TextView)19 Context (android.content.Context)18 SuppressLint (android.annotation.SuppressLint)17 SmallTest (android.test.suitebuilder.annotation.SmallTest)13 AllCapsTransformationMethod (android.text.method.AllCapsTransformationMethod)13 RippleDrawable (android.graphics.drawable.RippleDrawable)12 StateListDrawable (android.graphics.drawable.StateListDrawable)12 View (android.view.View)12 Nullable (android.annotation.Nullable)10 Bitmap (android.graphics.Bitmap)10 TextAppearanceSpan (android.text.style.TextAppearanceSpan)9 PorterDuff (android.graphics.PorterDuff)8