Java Code Examples for android.support.annotation.StyleableRes

The following are top voted examples for showing how to use android.support.annotation.StyleableRes. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: More-For-GO   File: ThemeUtils.java   View source code 6 votes vote down vote up
@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;
}
 
Example 2
Project: android-issue-reporter   File: ThemeUtils.java   View source code 6 votes vote down vote up
@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;
}
 
Example 3
Project: openwebnet-android   File: FontViewHelper.java   View source code 6 votes vote down vote up
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();
    }
}
 
Example 4
Project: the-ssn-app   File: ThemeUtils.java   View source code 6 votes vote down vote up
@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;
}
 
Example 5
Project: Loyalty   File: MultiMode.java   View source code 5 votes vote down vote up
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;
}
 
Example 6
Project: StickyScrollView   File: StickyScrollPresenter.java   View source code 5 votes vote down vote up
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();
}
 
Example 7
Project: letv   File: TypedArrayUtils.java   View source code 5 votes vote down vote up
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;
}
 
Example 8
Project: letv   File: TypedArrayUtils.java   View source code 5 votes vote down vote up
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;
}
 
Example 9
Project: letv   File: TypedArrayUtils.java   View source code 5 votes vote down vote up
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;
}
 
Example 10
Project: NumberPadTimePicker   File: NumberPadTimePickerBottomSheetComponent.java   View source code 5 votes vote down vote up
@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;
}
 
Example 11
Project: CameraButton   File: TypedArrayHelper.java   View source code 5 votes vote down vote up
@Px
static int getDimension(Context context,
                        TypedArray array,
                        @StyleableRes int attr,
                        @DimenRes int defaultDimenRes) {

    return array.getDimensionPixelOffset(
            attr, context.getResources().getDimensionPixelSize(defaultDimenRes));
}
 
Example 12
Project: CameraButton   File: TypedArrayHelper.java   View source code 5 votes vote down vote up
@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));
    }
}
 
Example 13
Project: CameraButton   File: TypedArrayHelper.java   View source code 5 votes vote down vote up
@ColorInt
static int[] getColors(Context context,
                       TypedArray array,
                       @StyleableRes int attr,
                       @ArrayRes int defaultColorsRes) {

    return context.getResources().getIntArray(
            array.getResourceId(attr, defaultColorsRes));
}
 
Example 14
Project: CameraButton   File: TypedArrayHelper.java   View source code 5 votes vote down vote up
static int getInteger(Context context,
                      TypedArray array,
                      @StyleableRes int attr,
                      @IntegerRes int defaultIntRes) {

    return array.getInteger(
            attr, context.getResources().getInteger(defaultIntRes));
}
 
Example 15
Project: boohee_v5.6   File: TypedArrayUtils.java   View source code 5 votes vote down vote up
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;
}
 
Example 16
Project: boohee_v5.6   File: TypedArrayUtils.java   View source code 5 votes vote down vote up
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;
}
 
Example 17
Project: boohee_v5.6   File: TypedArrayUtils.java   View source code 5 votes vote down vote up
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;
}
 
Example 18
Project: rview   File: FixedSizeLayout.java   View source code 5 votes vote down vote up
@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();
}
 
Example 19
Project: px-android   File: MPLoadingSpinner.java   View source code 5 votes vote down vote up
/**
 * 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;
}
 
Example 20
Project: BottomSheetPickers   File: NumberPadTimePickerBottomSheetComponent.java   View source code 5 votes vote down vote up
@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;
}
 
Example 21
Project: material-components-android   File: MotionSpec.java   View source code 5 votes vote down vote up
/**
 * 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;
}
 
Example 22
Project: material-components-android   File: MaterialResources.java   View source code 5 votes vote down vote up
/**
 * 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);
}
 
Example 23
Project: material-components-android   File: MaterialResources.java   View source code 5 votes vote down vote up
/**
 * 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);
}
 
Example 24
Project: material-components-android   File: MaterialResources.java   View source code 5 votes vote down vote up
/**
 * 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;
}
 
Example 25
Project: material-components-android   File: MaterialResources.java   View source code 5 votes vote down vote up
/**
 * 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;
}
 
Example 26
Project: amplify   File: CustomLayoutPromptViewConfig.java   View source code 5 votes vote down vote up
/**
 * @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;
}
 
Example 27
Project: amplify   File: DefaultLayoutPromptViewConfig.java   View source code 5 votes vote down vote up
/**
 * @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;
}
 
Example 28
Project: amplify   File: BasePromptViewConfig.java   View source code 5 votes vote down vote up
/**
 * @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;
}
 
Example 29
Project: Lock.Android   File: AuthConfig.java   View source code 5 votes vote down vote up
/**
 * 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;
}
 
Example 30
Project: tooltip-view   File: TooltipView.java   View source code 5 votes vote down vote up
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;
}
 
Example 31
Project: Android-Client   File: TextViewAttrHelper.java   View source code 4 votes vote down vote up
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);
}
 
Example 32
Project: StickyScrollView   File: ResourceProvider.java   View source code 4 votes vote down vote up
public ResourceProvider(Context context, AttributeSet attrs, @StyleableRes int[] styleRes) {
    mTypeArray = context.obtainStyledAttributes(attrs, styleRes);
}
 
Example 33
Project: StickyScrollView   File: ResourceProvider.java   View source code 4 votes vote down vote up
@Override
public int getResourceId(@StyleableRes int styleResId) {
    return mTypeArray.getResourceId(styleResId, 0);
}
 
Example 34
Project: letv   File: TypedArrayUtils.java   View source code 4 votes vote down vote up
public static boolean getBoolean(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, boolean defaultValue) {
    return a.getBoolean(index, a.getBoolean(fallbackIndex, defaultValue));
}
 
Example 35
Project: letv   File: TypedArrayUtils.java   View source code 4 votes vote down vote up
public static int getInt(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, int defaultValue) {
    return a.getInt(index, a.getInt(fallbackIndex, defaultValue));
}
 
Example 36
Project: letv   File: TypedArrayUtils.java   View source code 4 votes vote down vote up
@AnyRes
public static int getResourceId(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, @AnyRes int defaultValue) {
    return a.getResourceId(index, a.getResourceId(fallbackIndex, defaultValue));
}
 
Example 37
Project: FontProvider   File: IntegerSimpleMenuPreference.java   View source code 4 votes vote down vote up
@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);
}
 
Example 38
Project: boohee_v5.6   File: TypedArrayUtils.java   View source code 4 votes vote down vote up
public static boolean getBoolean(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, boolean defaultValue) {
    return a.getBoolean(index, a.getBoolean(fallbackIndex, defaultValue));
}
 
Example 39
Project: boohee_v5.6   File: TypedArrayUtils.java   View source code 4 votes vote down vote up
public static int getInt(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, int defaultValue) {
    return a.getInt(index, a.getInt(fallbackIndex, defaultValue));
}
 
Example 40
Project: boohee_v5.6   File: TypedArrayUtils.java   View source code 4 votes vote down vote up
@AnyRes
public static int getResourceId(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, @AnyRes int defaultValue) {
    return a.getResourceId(index, a.getResourceId(fallbackIndex, defaultValue));
}