public BaseTabItem(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { super(context, attrs, defStyleAttr); }
public static float getFloatFromAttrRes(@AttrRes int attrRes, float defaultValue, @NonNull Context context) { TypedArray a = context.obtainStyledAttributes(new int[] { attrRes }); try { return a.getFloat(0, defaultValue); } finally { a.recycle(); } } }
/** * Gets the required boolean value from the current context, if possible/available * @param context The context to use as reference for the boolean * @param attr Attribute id to resolve * @param fallback Default value to return if no value is specified in theme * @return the boolean value from current theme */ private static boolean resolveBoolean(Context context, @AttrRes int attr, boolean fallback) { TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr}); try { return a.getBoolean(0, fallback); } finally { a.recycle(); } }
public OnlyIconMaterialItemView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { super(context, attrs, defStyleAttr); LayoutInflater.from(context).inflate(R.layout.item_material_only_icon, this, true); mIcon = findViewById(R.id.icon); mMessages = findViewById(R.id.messages); }
@ColorInt public static int getColorFromAttrRes(@AttrRes int attrRes, @ColorInt int defaultValue, @NonNull Context context) { TypedArray a = context.obtainStyledAttributes(new int[] { attrRes }); try { return a.getColor(0, defaultValue); } finally { a.recycle(); } }
/** * Queries the theme of the given {@code context} for a theme color. * * @param context the context holding the current theme. * @param attrResId the theme color attribute to resolve. * @param fallbackColorResId a color resource id tto fallback to if the theme color cannot be * resolved. * @return the theme color or the fallback color. */ @ColorInt public static int getThemeColor(@NonNull Context context, @AttrRes int attrResId, @ColorRes int fallbackColorResId) { final TypedValue tv = new TypedValue(); if (context.getTheme().resolveAttribute(attrResId, tv, true)) { return tv.data; } return ContextCompat.getColor(context, fallbackColorResId); }
public MaterialItemView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { super(context, attrs, defStyleAttr); final float scale = context.getResources().getDisplayMetrics().density; mTranslation = scale * 2; mTranslationHideTitle = scale * 10; mTopMargin = (int) (scale * 8); mTopMarginHideTitle = (int) (scale * 16); LayoutInflater.from(context).inflate(R.layout.item_material, this, true); mIcon = findViewById(R.id.icon); mLabel = findViewById(R.id.label); mMessages = findViewById(R.id.messages); }
@UiThread // Implicit synchronization for use of shared resource VALUE. public static Drawable getTintedDrawable(Context context, @DrawableRes int id, @AttrRes int tintAttrId) { boolean attributeFound = context.getTheme().resolveAttribute(tintAttrId, VALUE, true); if (!attributeFound) { throw new Resources.NotFoundException("Required tint color attribute with name " + context.getResources().getResourceEntryName(tintAttrId) + " and attribute ID " + tintAttrId + " was not found."); } Drawable drawable = ContextCompat.getDrawable(context, id); drawable = DrawableCompat.wrap(drawable.mutate()); int color = ContextCompat.getColor(context, VALUE.resourceId); DrawableCompat.setTint(drawable, color); return drawable; }
/** * Creates a new tab switcher, which allows to switch between multiple tabs. * * @param context * The context, which should be used by the view, as an instance of the class {@link * Context}. The context may not be null * @param attributeSet * The attribute set, the view's attributes should be obtained from, as an instance of * the type {@link AttributeSet} or null, if no attributes should be obtained * @param defaultStyle * The default style to apply to this view. If 0, no style will be applied (beyond what * is included in the theme). This may either be an attribute resource, whose value will * be retrieved from the current theme, or an explicit style resource */ public TabSwitcher(@NonNull final Context context, @Nullable final AttributeSet attributeSet, @AttrRes final int defaultStyle) { super(context, attributeSet, defaultStyle); initialize(attributeSet, defaultStyle, 0); }
/** * Creates a new image button, which allows to display the number of tabs, which are currently * contained by a {@link TabSwitcher}. * * @param context * The context, which should be used by the view, as an instance of the class {@link * Context}. The context may not be null * @param attributeSet * The attribute set, the view's attributes should be obtained from, as an instance of * the type {@link AttributeSet} or null, if no attributes should be obtained * @param defaultStyle * The default style to apply to this view. If 0, no style will be applied (beyond what * is included in the theme). This may either be an attribute resource, whose value will * be retrieved from the current theme, or an explicit style resource */ public TabSwitcherButton(@NonNull final Context context, @Nullable final AttributeSet attributeSet, @AttrRes final int defaultStyle) { super(context, attributeSet, defaultStyle); initialize(); }
/** * Creates a new tab switcher, which allows to switch between multiple tabs. * * @param context * The context, which should be used by the view, as an instance of the class {@link * Context}. The context may not be null * @param attributeSet * The attribute set, the view's attributes should be obtained from, as an instance of * the type {@link AttributeSet} or null, if no attributes should be obtained * @param defaultStyle * The default style to apply to this view. If 0, no style will be applied (beyond what * is included in the theme). This may either be an attribute resource, whose value will * be retrieved from the current theme, or an explicit style resource * @param defaultStyleResource * A resource identifier of a style resource that supplies default values for the view, * used only if the default style is 0 or can not be found in the theme. Can be 0 to not * look for defaults */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) public TabSwitcher(@NonNull final Context context, @Nullable final AttributeSet attributeSet, @AttrRes final int defaultStyle, @StyleRes final int defaultStyleResource) { super(context, attributeSet, defaultStyle, defaultStyleResource); initialize(attributeSet, defaultStyle, defaultStyleResource); }
/** * Returns the text, which corresponds to a specific theme attribute, regarding the theme, which * is used when using a specific layout. * * @param layout * The layout as a value of the enum {@link Layout}. The layout may not be null * @param resourceId * The resource id of the theme attribute, the text should be obtained from, as an * {@link Integer} value. The resource id must correspond to a valid theme attribute * @return The text, which has been obtained, as an instance of the type {@link CharSequence} */ public CharSequence getText(@NonNull final Layout layout, @AttrRes final int resourceId) { try { return ThemeUtil.getText(context, resourceId); } catch (NotFoundException e1) { int themeResourceId = getThemeResourceId(layout); try { return ThemeUtil.getText(context, themeResourceId, resourceId); } catch (NotFoundException e) { themeResourceId = obtainThemeFromThemeAttributes(layout, themeResourceId); return ThemeUtil.getText(context, themeResourceId, resourceId); } } }
/** * Returns the drawable, which corresponds to a specific theme attribute, regarding the theme, * which is used when using a specific layout. * * @param layout * The layout as a value of the enum {@link Layout}. The layout may not be null * @param resourceId * The resource id of the theme attribute, the drawable should be obtained from, as an * {@link Integer} value. The resource id must correspond to a valid theme attribute * @return The color state list, which has been obtained, as an instance of the class {@link * ColorStateList} */ public Drawable getDrawable(@NonNull final Layout layout, @AttrRes final int resourceId) { try { return ThemeUtil.getDrawable(context, resourceId); } catch (NotFoundException e1) { int themeResourceId = getThemeResourceId(layout); try { return ThemeUtil.getDrawable(context, themeResourceId, resourceId); } catch (NotFoundException e) { themeResourceId = obtainThemeFromThemeAttributes(layout, themeResourceId); return ThemeUtil.getDrawable(context, themeResourceId, resourceId); } } }
/** * Returns the integer value, which corresponds to a specific theme attribute, regarding the * theme, which is used when using a specific layout. * * @param layout * The layout as a value of the enum {@link Layout}. The layout may not be null * @param resourceId * The resource id of the theme attribute, the integer value should be obtained from, as * an {@link Integer} value. The resource id must correspond to a valid theme attribute * @param defaultValue * The default value, which should be returned, if the given resource id is invalid, as * an {@link Integer} value * @return The integer value, which has been obtained, as an {@link Integer} value */ public int getInteger(@NonNull final Layout layout, @AttrRes final int resourceId, final int defaultValue) { int result = ThemeUtil.getInt(context, resourceId, 0); if (result == 0) { int themeResourceId = getThemeResourceId(layout); result = ThemeUtil.getInt(context, themeResourceId, resourceId, 0); if (result == 0) { themeResourceId = obtainThemeFromThemeAttributes(layout, themeResourceId); return ThemeUtil.getInt(context, themeResourceId, resourceId, defaultValue); } } return result; }
/** * Returns the color state list, which corresponds to a specific theme attribute, regarding the * theme, which is used when using a specific layout. * * @param layout * The layout as a value of the enum {@link Layout}. The layout may not be null * @param resourceId * The resource id of the theme attribute, the color state list should be obtained from, * as an {@link Integer} value. The resource id must correspond to a valid theme * attribute * @return The color state list, which has been obtained, as an instance of the class {@link * ColorStateList} */ public ColorStateList getColorStateList(@NonNull final Layout layout, @AttrRes final int resourceId) { try { return ThemeUtil.getColorStateList(context, resourceId); } catch (NotFoundException e1) { int themeResourceId = getThemeResourceId(layout); try { return ThemeUtil.getColorStateList(context, themeResourceId, resourceId); } catch (NotFoundException e) { themeResourceId = obtainThemeFromThemeAttributes(layout, themeResourceId); return ThemeUtil.getColorStateList(context, themeResourceId, resourceId); } } }
/** * Returns the resource id, which corresponds to a specific theme attribute, regarding the * theme, which is used when using a specific layout. * * @param layout * The layout as a value of the enum {@link Layout}. The layout may not be null * @param resourceId * The resource id of the theme attribute, the resource id should be obtained from, as * an {@link Integer} value. The resource id must correspond to a valid theme attribute * @param defaultValue * The default value, which should be returned, if the given resource id is invalid, as * an {@link Integer} value * @return The resource id, which has been obtained, as an {@link Integer} value */ public int getResourceId(@NonNull final Layout layout, @AttrRes final int resourceId, final int defaultValue) { int result = ThemeUtil.getResId(context, resourceId, 0); if (result == 0) { int themeResourceId = getThemeResourceId(layout); result = ThemeUtil.getResId(context, themeResourceId, resourceId, 0); if (result == 0) { themeResourceId = obtainThemeFromThemeAttributes(layout, themeResourceId); return ThemeUtil.getResId(context, themeResourceId, resourceId, defaultValue); } } return result; }
/** * Returns the color, which corresponds to a specific theme attribute, regarding the theme, * which is used when using a specific layout. * * @param layout * The layout as a value of the enum {@link Layout}. The layout may not be null * @param resourceId * The resource id of the theme attribute, the color should be obtained from, as an * {@link Integer} value. The resource id must correspond to a valid theme attribute * @return The color, which has been obtained, as an {@link Integer} value */ @ColorInt public int getColor(@NonNull final Layout layout, @AttrRes final int resourceId) { try { return ThemeUtil.getColor(context, resourceId); } catch (NotFoundException e1) { int themeResourceId = getThemeResourceId(layout); try { return ThemeUtil.getColor(context, themeResourceId, resourceId); } catch (NotFoundException e) { themeResourceId = obtainThemeFromThemeAttributes(layout, themeResourceId); return ThemeUtil.getColor(context, themeResourceId, resourceId); } } }
/** * Initializes the view. * * @param attributeSet * The attribute set, which should be used to initialize the view, as an instance of the * type {@link AttributeSet} or null, if no attributes should be obtained * @param defaultStyle * The default style to apply to this view. If 0, no style will be applied (beyond what * is included in the theme). This may either be an attribute resource, whose value will * be retrieved from the current theme, or an explicit style resource * @param defaultStyleResource * A resource identifier of a style resource that supplies default values for the view, * used only if the default style is 0 or can not be found in the theme. Can be 0 to not * look for defaults */ private void initialize(@Nullable final AttributeSet attributeSet, @AttrRes final int defaultStyle, @StyleRes final int defaultStyleResource) { pendingActions = new LinkedList<>(); listeners = new CopyOnWriteArraySet<>(); model = new TabSwitcherModel(this); model.addListener(createModelListener()); touchEventDispatcher = new TouchEventDispatcher(); setPadding(super.getPaddingLeft(), super.getPaddingTop(), super.getPaddingRight(), super.getPaddingBottom()); obtainStyledAttributes(attributeSet, defaultStyle, defaultStyleResource); getViewTreeObserver().addOnGlobalLayoutListener( new LayoutListenerWrapper(this, createGlobalLayoutListener(false))); }
@AttrRes final int defaultStyle, @StyleRes final int defaultStyleResource) { TypedArray typedArray = getContext()
@ColorInt public static int getColorByAttribute(@AttrRes int resId, @AttrRes int altResId, @NonNull Resources.Theme theme) throws Resources.NotFoundException { TypedValue value = new TypedValue(); if (!theme.resolveAttribute(resId, value, true) && !theme.resolveAttribute(altResId, value, true)) { throw new Resources.NotFoundException( "Failed to resolve attribute IDs #0x" + Integer.toHexString(resId) + "and " + Integer.toHexString(altResId) + " type #0x" + Integer.toHexString(value.type)); } return value.data; } }