@ColorInt private static int[] resolveThemeColors(@NonNull Context context, @AttrRes @StyleableRes int[] attrs, @ColorInt int[] defaultColors) { if (attrs.length != defaultColors.length) throw new IllegalArgumentException("Argument attrs must be the same size as defaultColors"); TypedValue typedValue = new TypedValue(); TypedArray a = context.obtainStyledAttributes(typedValue.data, attrs); for (int i = 0; i < attrs.length; i++) { defaultColors[i] = a.getColor(0, defaultColors[i]); } a.recycle(); return defaultColors; }
public static void initCustomFont(TextView view, AttributeSet attributeSet, @StyleableRes int[] attrs, int attrIndex) { TypedArray typedArray = view.getContext().getTheme() .obtainStyledAttributes(attributeSet, attrs, 0, 0); try { int fontIndex = typedArray.getInt(attrIndex, DEFAULT_FONT); String fontPath = FONTS.get(fontIndex); if (fontPath != null) { view.setTypeface(Typeface.createFromAsset(view.getContext().getAssets(), fontPath)); } else { throw new IllegalArgumentException("invalid font path"); } } finally { typedArray.recycle(); } }
@ColorInt private static int[] resolveThemeColors(@NonNull Context context, @AttrRes @StyleableRes int[] attrs, @ColorInt int[] defaultColors) { if (attrs.length != defaultColors.length) throw new IllegalArgumentException("Argument attrs must be the same size as defaultColors"); TypedValue typedValue = new TypedValue(); //noinspection ResourceType TypedArray a = context.obtainStyledAttributes(typedValue.data, attrs); for (int i = 0; i < attrs.length; i++) { defaultColors[i] = a.getColor(0, defaultColors[i]); } a.recycle(); return defaultColors; }
private ToolbarState initPrevState(Toolbar toolbar) { ToolbarState prevState = new ToolbarState(); if (toolbar.getBackground() != null) { prevState.toolbarColor = ((ColorDrawable) (toolbar.getBackground())).getColor(); } Context context = toolbar.getContext(); TypedValue typedValue = new TypedValue(); TypedArray attr = context.obtainStyledAttributes(typedValue.data, new int[]{R.attr.colorPrimary, R.attr.colorPrimaryDark}); if (attr != null) { if (prevState.toolbarColor > 0) { prevState.toolbarColor = attr.getColor(0, 0); } @StyleableRes int index = 1; prevState.statusBarColor = attr.getColor(index, prevState.toolbarColor); attr.recycle(); } if (toolbar.getTitle() != null) { prevState.title = toolbar.getTitle().toString(); } if (toolbar.getSubtitle() != null) { prevState.subTitle = toolbar.getSubtitle().toString(); } prevState.title = prevState.title != null ? prevState.title : EMPTY; prevState.subTitle = prevState.subTitle != null ? prevState.subTitle : EMPTY; prevState.logo = toolbar.getLogo(); prevState.navigationIcon = toolbar.getNavigationIcon(); return prevState; }
public void onGlobalLayoutChange(@StyleableRes int headerRes, @StyleableRes int footerRes){ int headerId = mTypedArrayResourceProvider.getResourceId(headerRes); if(headerId != 0) { mStickyScrollPresentation.initHeaderView(headerId); } int footerId = mTypedArrayResourceProvider.getResourceId(footerRes); if(footerId != 0){ mStickyScrollPresentation.initFooterView(footerId); } mTypedArrayResourceProvider.recycle(); }
public static Drawable getDrawable(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex) { Drawable val = a.getDrawable(index); if (val == null) { return a.getDrawable(fallbackIndex); } return val; }
public static String getString(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex) { String val = a.getString(index); if (val == null) { return a.getString(fallbackIndex); } return val; }
public static CharSequence[] getTextArray(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex) { CharSequence[] val = a.getTextArray(index); if (val == null) { return a.getTextArray(fallbackIndex); } return val; }
@NonNull private static int[] resolveColorAttributesFromTheme(Context context, @StyleableRes int[] attrs) { final TypedArray ta = context.obtainStyledAttributes(attrs); final int[] colors = new int[attrs.length]; for (int idxAttr = 0; idxAttr < colors.length; idxAttr++) { colors[idxAttr] = ta.getColor(idxAttr, 0); } ta.recycle(); return colors; }
@Px static int getDimension(Context context, TypedArray array, @StyleableRes int attr, @DimenRes int defaultDimenRes) { return array.getDimensionPixelOffset( attr, context.getResources().getDimensionPixelSize(defaultDimenRes)); }
@ColorInt static int getColor(Context context, TypedArray array, @StyleableRes int attr, @ColorRes int defaultColorRes) { if (Build.VERSION.SDK_INT >= 23) { return array.getColor(attr, context.getColor(defaultColorRes)); } else { return array.getColor(attr, context.getResources().getColor(defaultColorRes)); } }
@ColorInt static int[] getColors(Context context, TypedArray array, @StyleableRes int attr, @ArrayRes int defaultColorsRes) { return context.getResources().getIntArray( array.getResourceId(attr, defaultColorsRes)); }
static int getInteger(Context context, TypedArray array, @StyleableRes int attr, @IntegerRes int defaultIntRes) { return array.getInteger( attr, context.getResources().getInteger(defaultIntRes)); }
@SuppressWarnings("ResourceType") public FixedSizeLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); @StyleableRes int[] sizeAttrs = { android.R.attr.maxWidth, android.R.attr.maxHeight }; TypedArray a = context.obtainStyledAttributes(attrs, sizeAttrs); mMaxLayoutWidth = a.getLayoutDimension(0, -1); mMaxLayoutHeight = a.getLayoutDimension(1, -1); a.recycle(); }
/** * Load the color from xml attributes or fallback to the default if not found * * @param typedArray attributes typed array * @param index the styleable index * @param defaultColor the default color resource id * @return the color to use */ @ColorInt private int loadColor(TypedArray typedArray, @StyleableRes int index, @ColorRes int defaultColor) { int loadedColor = ContextCompat.getColor(getContext(), defaultColor); ColorStateList colorList = typedArray.getColorStateList(index); if (colorList != null) { loadedColor = colorList.getDefaultColor(); } return loadedColor; }
/** * Inflates an instance of MotionSpec from the animator resource indexed in the given attributes * array. */ @Nullable public static MotionSpec createFromAttribute( Context context, TypedArray attributes, @StyleableRes int index) { if (attributes.hasValue(index)) { int resourceId = attributes.getResourceId(index, 0); if (resourceId != 0) { return createFromResource(context, resourceId); } } return null; }
/** * Returns the {@link ColorStateList} from the given attributes. The resource can include * themeable attributes, regardless of API level. */ @Nullable public static ColorStateList getColorStateList( Context context, TypedArray attributes, @StyleableRes int index) { if (attributes.hasValue(index)) { int resourceId = attributes.getResourceId(index, 0); if (resourceId != 0) { ColorStateList value = AppCompatResources.getColorStateList(context, resourceId); if (value != null) { return value; } } } return attributes.getColorStateList(index); }
/** * Returns the drawable object from the given attributes. * * <p>This method supports inflation of {@code <vector>} and {@code <animated-vector>} resources * on devices where platform support is not available. */ @Nullable public static Drawable getDrawable( Context context, TypedArray attributes, @StyleableRes int index) { if (attributes.hasValue(index)) { int resourceId = attributes.getResourceId(index, 0); if (resourceId != 0) { Drawable value = AppCompatResources.getDrawable(context, resourceId); if (value != null) { return value; } } } return attributes.getDrawable(index); }
/** * Returns a TextAppearanceSpan object from the given attributes. * * <p>You only need this if you are drawing text manually. Normally, TextView takes care of this. */ @Nullable public static TextAppearance getTextAppearance( Context context, TypedArray attributes, @StyleableRes int index) { if (attributes.hasValue(index)) { int resourceId = attributes.getResourceId(index, 0); if (resourceId != 0) { return new TextAppearance(context, resourceId); } } return null; }
/** * Returns the @StyleableRes index that contains value in the attributes array. If both indices * contain values, the first given index takes precedence and is returned. */ @StyleableRes static int getIndexWithValue(TypedArray attributes, @StyleableRes int a, @StyleableRes int b) { if (attributes.hasValue(a)) { return a; } return b; }
/** * @return the color value for the attribute at <code>index</code>, if defined; null otherwise */ @Nullable @LayoutRes private static Integer suppliedLayoutOrNull(@NonNull final TypedArray typedArray, @StyleableRes final int index) { final int layoutResourceId = typedArray.getResourceId(index, DEFAULT_LAYOUT_RES_ID_IF_UNDEFINED); //noinspection ResourceType return layoutResourceId != DEFAULT_LAYOUT_RES_ID_IF_UNDEFINED ? layoutResourceId : null; }
/** * @return the dimension in px defined for the attribute at <code>index</code>, if defined; null otherwise */ @Nullable @Px private static Integer suppliedDimensionOrNull( @NonNull final TypedArray typedArray, @StyleableRes final int index) { final int dimensionPixelSize = typedArray.getDimensionPixelSize(index, DEFAULT_DIMENSION_VALUE_IF_UNDEFINED); //noinspection ResourceType return dimensionPixelSize != DEFAULT_DIMENSION_VALUE_IF_UNDEFINED ? dimensionPixelSize : null; }
/** * @return the long value for the attribute at <code>index</code>, if defined; null otherwise */ @Nullable private static Long suppliedLongOrNull( @Nullable final TypedArray typedArray, @StyleableRes final int index) { if (typedArray != null) { final int integer = typedArray.getInt(index, DEFAULT_INTEGER_VALUE_IF_UNDEFINED); return integer != DEFAULT_INTEGER_VALUE_IF_UNDEFINED ? (long) integer : null; } return null; }
/** * Retrieves the resource id of the given Style index. * * @param context a valid Context * @param index The index to search on the Style definition. * @return the id if found or -1. */ int getIdForResource(@NonNull Context context, @StyleableRes int index) { final int[] attrs = new int[]{index}; final TypedArray typedArray = context.getTheme().obtainStyledAttributes(styleRes, attrs); int id = typedArray.getResourceId(0, -1); typedArray.recycle(); return id; }
private int getDimension(TypedArray a, @StyleableRes int styleableId, @DimenRes int defaultDimension) { int result = a.getDimensionPixelSize(styleableId, NOT_PRESENT); if (result == NOT_PRESENT) { result = getResources().getDimensionPixelSize(defaultDimension); } return result; }
public static void applyAttributes(TextView textView, @Nullable AttributeSet attrs, @StyleableRes int[] styleableIds, @StyleableRes int fontStyableId) { Context context = textView.getContext(); Typeface typeface = Font.MEDIUM.getTypeface(context); if (attrs != null) { TypedArray typedArray = context.obtainStyledAttributes(attrs, styleableIds); int fontId = typedArray.getInt(fontStyableId, Font.MEDIUM.getId()); typeface = Font.fromId(fontId).getTypeface(context); typedArray.recycle(); } textView.setTypeface(typeface); }
public ResourceProvider(Context context, AttributeSet attrs, @StyleableRes int[] styleRes) { mTypeArray = context.obtainStyledAttributes(attrs, styleRes); }
@Override public int getResourceId(@StyleableRes int styleResId) { return mTypeArray.getResourceId(styleResId, 0); }
public static boolean getBoolean(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, boolean defaultValue) { return a.getBoolean(index, a.getBoolean(fallbackIndex, defaultValue)); }
public static int getInt(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, int defaultValue) { return a.getInt(index, a.getInt(fallbackIndex, defaultValue)); }
@AnyRes public static int getResourceId(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, @AnyRes int defaultValue) { return a.getResourceId(index, a.getResourceId(fallbackIndex, defaultValue)); }
@SuppressLint("RestrictedApi") private static int[] getIntArray(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex) { int resourceId = TypedArrayUtils.getResourceId(a, index, fallbackIndex, 0); return a.getResources().getIntArray(resourceId); }