Search in sources :

Example 86 with TypedValue

use of android.util.TypedValue in project robolectric by robolectric.

the class ShadowThemeTest method failToResolveCircularReference.

@Test
public void failToResolveCircularReference() throws Exception {
    TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get();
    TypedValue value = new TypedValue();
    boolean resolved = activity.getTheme().resolveAttribute(R.attr.isSugary, value, false);
    assertThat(resolved).isFalse();
}
Also used : TypedValue(android.util.TypedValue) Test(org.junit.Test)

Example 87 with TypedValue

use of android.util.TypedValue in project robolectric by robolectric.

the class ShadowThemeTest method canResolveAttrReferenceToDifferentPackage.

@Test
public void canResolveAttrReferenceToDifferentPackage() throws Exception {
    TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get();
    TypedValue value = new TypedValue();
    boolean resolved = activity.getTheme().resolveAttribute(R.attr.styleReference, value, false);
    assertThat(resolved).isTrue();
    assertThat(value.type).isEqualTo(TypedValue.TYPE_REFERENCE);
    assertThat(value.data).isEqualTo(R.style.Widget_AnotherTheme_Button);
}
Also used : TypedValue(android.util.TypedValue) Test(org.junit.Test)

Example 88 with TypedValue

use of android.util.TypedValue in project robolectric by robolectric.

the class ShadowThemeTest method withResolveRefsFalse_shouldNotResolveResource.

@Test
public void withResolveRefsFalse_shouldNotResolveResource() throws Exception {
    TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get();
    TypedValue value = new TypedValue();
    boolean resolved = activity.getTheme().resolveAttribute(R.attr.logoHeight, value, false);
    assertThat(resolved).isTrue();
    assertThat(value.type).isEqualTo(TypedValue.TYPE_REFERENCE);
    assertThat(value.data).isEqualTo(R.dimen.test_dp_dimen);
}
Also used : TypedValue(android.util.TypedValue) Test(org.junit.Test)

Example 89 with TypedValue

use of android.util.TypedValue in project MaterialEditText by rengwuxian.

the class MaterialAutoCompleteTextView method init.

private void init(Context context, AttributeSet attrs) {
    iconSize = getPixel(32);
    iconOuterWidth = getPixel(48);
    iconOuterHeight = getPixel(32);
    bottomSpacing = getResources().getDimensionPixelSize(R.dimen.inner_components_spacing);
    bottomEllipsisSize = getResources().getDimensionPixelSize(R.dimen.bottom_ellipsis_height);
    // default baseColor is black
    int defaultBaseColor = Color.BLACK;
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialEditText);
    textColorStateList = typedArray.getColorStateList(R.styleable.MaterialEditText_met_textColor);
    textColorHintStateList = typedArray.getColorStateList(R.styleable.MaterialEditText_met_textColorHint);
    baseColor = typedArray.getColor(R.styleable.MaterialEditText_met_baseColor, defaultBaseColor);
    // retrieve the default primaryColor
    int defaultPrimaryColor;
    TypedValue primaryColorTypedValue = new TypedValue();
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            context.getTheme().resolveAttribute(android.R.attr.colorPrimary, primaryColorTypedValue, true);
            defaultPrimaryColor = primaryColorTypedValue.data;
        } else {
            throw new RuntimeException("SDK_INT less than LOLLIPOP");
        }
    } catch (Exception e) {
        try {
            int colorPrimaryId = getResources().getIdentifier("colorPrimary", "attr", getContext().getPackageName());
            if (colorPrimaryId != 0) {
                context.getTheme().resolveAttribute(colorPrimaryId, primaryColorTypedValue, true);
                defaultPrimaryColor = primaryColorTypedValue.data;
            } else {
                throw new RuntimeException("colorPrimary not found");
            }
        } catch (Exception e1) {
            defaultPrimaryColor = baseColor;
        }
    }
    primaryColor = typedArray.getColor(R.styleable.MaterialEditText_met_primaryColor, defaultPrimaryColor);
    setFloatingLabelInternal(typedArray.getInt(R.styleable.MaterialEditText_met_floatingLabel, 0));
    errorColor = typedArray.getColor(R.styleable.MaterialEditText_met_errorColor, Color.parseColor("#e7492E"));
    minCharacters = typedArray.getInt(R.styleable.MaterialEditText_met_minCharacters, 0);
    maxCharacters = typedArray.getInt(R.styleable.MaterialEditText_met_maxCharacters, 0);
    singleLineEllipsis = typedArray.getBoolean(R.styleable.MaterialEditText_met_singleLineEllipsis, false);
    helperText = typedArray.getString(R.styleable.MaterialEditText_met_helperText);
    helperTextColor = typedArray.getColor(R.styleable.MaterialEditText_met_helperTextColor, -1);
    minBottomTextLines = typedArray.getInt(R.styleable.MaterialEditText_met_minBottomTextLines, 0);
    String fontPathForAccent = typedArray.getString(R.styleable.MaterialEditText_met_accentTypeface);
    if (fontPathForAccent != null && !isInEditMode()) {
        accentTypeface = getCustomTypeface(fontPathForAccent);
        textPaint.setTypeface(accentTypeface);
    }
    String fontPathForView = typedArray.getString(R.styleable.MaterialEditText_met_typeface);
    if (fontPathForView != null && !isInEditMode()) {
        typeface = getCustomTypeface(fontPathForView);
        setTypeface(typeface);
    }
    floatingLabelText = typedArray.getString(R.styleable.MaterialEditText_met_floatingLabelText);
    if (floatingLabelText == null) {
        floatingLabelText = getHint();
    }
    floatingLabelPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_floatingLabelPadding, bottomSpacing);
    floatingLabelTextSize = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_floatingLabelTextSize, getResources().getDimensionPixelSize(R.dimen.floating_label_text_size));
    floatingLabelTextColor = typedArray.getColor(R.styleable.MaterialEditText_met_floatingLabelTextColor, -1);
    floatingLabelAnimating = typedArray.getBoolean(R.styleable.MaterialEditText_met_floatingLabelAnimating, true);
    bottomTextSize = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_bottomTextSize, getResources().getDimensionPixelSize(R.dimen.bottom_text_size));
    hideUnderline = typedArray.getBoolean(R.styleable.MaterialEditText_met_hideUnderline, false);
    underlineColor = typedArray.getColor(R.styleable.MaterialEditText_met_underlineColor, -1);
    autoValidate = typedArray.getBoolean(R.styleable.MaterialEditText_met_autoValidate, false);
    iconLeftBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_met_iconLeft, -1));
    iconRightBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_met_iconRight, -1));
    showClearButton = typedArray.getBoolean(R.styleable.MaterialEditText_met_clearButton, false);
    clearButtonBitmaps = generateIconBitmaps(R.drawable.met_ic_clear);
    iconPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_iconPadding, getPixel(16));
    floatingLabelAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_met_floatingLabelAlwaysShown, false);
    helperTextAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_met_helperTextAlwaysShown, false);
    validateOnFocusLost = typedArray.getBoolean(R.styleable.MaterialEditText_met_validateOnFocusLost, false);
    checkCharactersCountAtBeginning = typedArray.getBoolean(R.styleable.MaterialEditText_met_checkCharactersCountAtBeginning, true);
    typedArray.recycle();
    int[] paddings = new int[] { // 0
    android.R.attr.padding, // 1
    android.R.attr.paddingLeft, // 2
    android.R.attr.paddingTop, // 3
    android.R.attr.paddingRight, // 4
    android.R.attr.paddingBottom };
    TypedArray paddingsTypedArray = context.obtainStyledAttributes(attrs, paddings);
    int padding = paddingsTypedArray.getDimensionPixelSize(0, 0);
    innerPaddingLeft = paddingsTypedArray.getDimensionPixelSize(1, padding);
    innerPaddingTop = paddingsTypedArray.getDimensionPixelSize(2, padding);
    innerPaddingRight = paddingsTypedArray.getDimensionPixelSize(3, padding);
    innerPaddingBottom = paddingsTypedArray.getDimensionPixelSize(4, padding);
    paddingsTypedArray.recycle();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        setBackground(null);
    } else {
        setBackgroundDrawable(null);
    }
    if (singleLineEllipsis) {
        TransformationMethod transformationMethod = getTransformationMethod();
        setSingleLine();
        setTransformationMethod(transformationMethod);
    }
    initMinBottomLines();
    initPadding();
    initText();
    initFloatingLabel();
    initTextWatcher();
    checkCharactersCount();
}
Also used : TypedArray(android.content.res.TypedArray) TransformationMethod(android.text.method.TransformationMethod) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) TypedValue(android.util.TypedValue)

Example 90 with TypedValue

use of android.util.TypedValue in project MaterialEditText by rengwuxian.

the class MaterialEditText method init.

private void init(Context context, AttributeSet attrs) {
    if (isInEditMode()) {
        return;
    }
    iconSize = getPixel(32);
    iconOuterWidth = getPixel(48);
    iconOuterHeight = getPixel(32);
    bottomSpacing = getResources().getDimensionPixelSize(R.dimen.inner_components_spacing);
    bottomEllipsisSize = getResources().getDimensionPixelSize(R.dimen.bottom_ellipsis_height);
    // default baseColor is black
    int defaultBaseColor = Color.BLACK;
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialEditText);
    textColorStateList = typedArray.getColorStateList(R.styleable.MaterialEditText_met_textColor);
    textColorHintStateList = typedArray.getColorStateList(R.styleable.MaterialEditText_met_textColorHint);
    baseColor = typedArray.getColor(R.styleable.MaterialEditText_met_baseColor, defaultBaseColor);
    // retrieve the default primaryColor
    int defaultPrimaryColor;
    TypedValue primaryColorTypedValue = new TypedValue();
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            context.getTheme().resolveAttribute(android.R.attr.colorPrimary, primaryColorTypedValue, true);
            defaultPrimaryColor = primaryColorTypedValue.data;
        } else {
            throw new RuntimeException("SDK_INT less than LOLLIPOP");
        }
    } catch (Exception e) {
        try {
            int colorPrimaryId = getResources().getIdentifier("colorPrimary", "attr", getContext().getPackageName());
            if (colorPrimaryId != 0) {
                context.getTheme().resolveAttribute(colorPrimaryId, primaryColorTypedValue, true);
                defaultPrimaryColor = primaryColorTypedValue.data;
            } else {
                throw new RuntimeException("colorPrimary not found");
            }
        } catch (Exception e1) {
            defaultPrimaryColor = baseColor;
        }
    }
    primaryColor = typedArray.getColor(R.styleable.MaterialEditText_met_primaryColor, defaultPrimaryColor);
    setFloatingLabelInternal(typedArray.getInt(R.styleable.MaterialEditText_met_floatingLabel, 0));
    errorColor = typedArray.getColor(R.styleable.MaterialEditText_met_errorColor, Color.parseColor("#e7492E"));
    minCharacters = typedArray.getInt(R.styleable.MaterialEditText_met_minCharacters, 0);
    maxCharacters = typedArray.getInt(R.styleable.MaterialEditText_met_maxCharacters, 0);
    singleLineEllipsis = typedArray.getBoolean(R.styleable.MaterialEditText_met_singleLineEllipsis, false);
    helperText = typedArray.getString(R.styleable.MaterialEditText_met_helperText);
    helperTextColor = typedArray.getColor(R.styleable.MaterialEditText_met_helperTextColor, -1);
    minBottomTextLines = typedArray.getInt(R.styleable.MaterialEditText_met_minBottomTextLines, 0);
    String fontPathForAccent = typedArray.getString(R.styleable.MaterialEditText_met_accentTypeface);
    if (fontPathForAccent != null && !isInEditMode()) {
        accentTypeface = getCustomTypeface(fontPathForAccent);
        textPaint.setTypeface(accentTypeface);
    }
    String fontPathForView = typedArray.getString(R.styleable.MaterialEditText_met_typeface);
    if (fontPathForView != null && !isInEditMode()) {
        typeface = getCustomTypeface(fontPathForView);
        setTypeface(typeface);
    }
    floatingLabelText = typedArray.getString(R.styleable.MaterialEditText_met_floatingLabelText);
    if (floatingLabelText == null) {
        floatingLabelText = getHint();
    }
    floatingLabelPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_floatingLabelPadding, bottomSpacing);
    floatingLabelTextSize = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_floatingLabelTextSize, getResources().getDimensionPixelSize(R.dimen.floating_label_text_size));
    floatingLabelTextColor = typedArray.getColor(R.styleable.MaterialEditText_met_floatingLabelTextColor, -1);
    floatingLabelAnimating = typedArray.getBoolean(R.styleable.MaterialEditText_met_floatingLabelAnimating, true);
    bottomTextSize = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_bottomTextSize, getResources().getDimensionPixelSize(R.dimen.bottom_text_size));
    hideUnderline = typedArray.getBoolean(R.styleable.MaterialEditText_met_hideUnderline, false);
    underlineColor = typedArray.getColor(R.styleable.MaterialEditText_met_underlineColor, -1);
    autoValidate = typedArray.getBoolean(R.styleable.MaterialEditText_met_autoValidate, false);
    iconLeftBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_met_iconLeft, -1));
    iconRightBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_met_iconRight, -1));
    showClearButton = typedArray.getBoolean(R.styleable.MaterialEditText_met_clearButton, false);
    clearButtonBitmaps = generateIconBitmaps(R.drawable.met_ic_clear);
    iconPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_iconPadding, getPixel(16));
    floatingLabelAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_met_floatingLabelAlwaysShown, false);
    helperTextAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_met_helperTextAlwaysShown, false);
    validateOnFocusLost = typedArray.getBoolean(R.styleable.MaterialEditText_met_validateOnFocusLost, false);
    checkCharactersCountAtBeginning = typedArray.getBoolean(R.styleable.MaterialEditText_met_checkCharactersCountAtBeginning, true);
    typedArray.recycle();
    int[] paddings = new int[] { // 0
    android.R.attr.padding, // 1
    android.R.attr.paddingLeft, // 2
    android.R.attr.paddingTop, // 3
    android.R.attr.paddingRight, // 4
    android.R.attr.paddingBottom };
    TypedArray paddingsTypedArray = context.obtainStyledAttributes(attrs, paddings);
    int padding = paddingsTypedArray.getDimensionPixelSize(0, 0);
    innerPaddingLeft = paddingsTypedArray.getDimensionPixelSize(1, padding);
    innerPaddingTop = paddingsTypedArray.getDimensionPixelSize(2, padding);
    innerPaddingRight = paddingsTypedArray.getDimensionPixelSize(3, padding);
    innerPaddingBottom = paddingsTypedArray.getDimensionPixelSize(4, padding);
    paddingsTypedArray.recycle();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        setBackground(null);
    } else {
        setBackgroundDrawable(null);
    }
    if (singleLineEllipsis) {
        TransformationMethod transformationMethod = getTransformationMethod();
        setSingleLine();
        setTransformationMethod(transformationMethod);
    }
    initMinBottomLines();
    initPadding();
    initText();
    initFloatingLabel();
    initTextWatcher();
    checkCharactersCount();
}
Also used : TypedArray(android.content.res.TypedArray) TransformationMethod(android.text.method.TransformationMethod) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) TypedValue(android.util.TypedValue)

Aggregations

TypedValue (android.util.TypedValue)844 TypedArray (android.content.res.TypedArray)190 Resources (android.content.res.Resources)92 ContextThemeWrapper (android.view.ContextThemeWrapper)52 Context (android.content.Context)50 Drawable (android.graphics.drawable.Drawable)49 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)46 TextView (android.widget.TextView)44 Paint (android.graphics.Paint)41 IOException (java.io.IOException)39 AttributeSet (android.util.AttributeSet)34 View (android.view.View)30 XmlResourceParser (android.content.res.XmlResourceParser)29 Point (android.graphics.Point)26 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)26 ColorStateList (android.content.res.ColorStateList)23 DisplayMetrics (android.util.DisplayMetrics)23 LinearLayout (android.widget.LinearLayout)20 Bundle (android.os.Bundle)19 SpannableString (android.text.SpannableString)19