Example usage for android.content.res TypedArray getResourceId

List of usage examples for android.content.res TypedArray getResourceId

Introduction

In this page you can find the example usage for android.content.res TypedArray getResourceId.

Prototype

@AnyRes
public int getResourceId(@StyleableRes int index, int defValue) 

Source Link

Document

Retrieves the resource identifier for the attribute at index.

Usage

From source file:android.support.v7ox.app.AppCompatViewInflater.java

/**
 * Allows us to emulate the {@code android:theme} attribute for devices before L.
 *//*  w  w  w. j a  va  2 s .  co  m*/
private static Context themifyContext(Context context, AttributeSet attrs, boolean useAndroidTheme,
        boolean useAppTheme) {
    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.View, 0, 0);
    int themeId = 0;
    if (useAndroidTheme) {
        // First try reading android:theme if enabled
        themeId = a.getResourceId(R.styleable.View_android_theme, 0);
    }
    if (useAppTheme && themeId == 0) {
        // ...if that didn't work, try reading app:theme (for legacy reasons) if enabled
        themeId = a.getResourceId(R.styleable.View_theme_ox, 0);

        if (themeId != 0) {
            Log.i(LOG_TAG, "app:theme is now deprecated. " + "Please move to using android:theme instead.");
        }
    }
    a.recycle();

    if (themeId != 0 && (!(context instanceof ContextThemeWrapper)
            || ((ContextThemeWrapper) context).getThemeResId() != themeId)) {
        // If the context isn't a ContextThemeWrapper, or it is but does not have
        // the same theme as we need, wrap it in a new wrapper
        context = new ContextThemeWrapper(context, themeId);
    }
    return context;
}

From source file:com.appeaser.sublimepickerlibrary.utilities.SUtils.java

public static ContextThemeWrapper createThemeWrapper(Context context, int parentStyleAttr,
        int parentDefaultStyle, int childStyleAttr, int childDefaultStyle) {
    final TypedArray forParent = context.obtainStyledAttributes(new int[] { parentStyleAttr });
    int parentStyle = forParent.getResourceId(0, parentDefaultStyle);
    forParent.recycle();//  w w  w  .ja  v a2  s  .  c om

    TypedArray forChild = context.obtainStyledAttributes(parentStyle, new int[] { childStyleAttr });
    int childStyleId = forChild.getResourceId(0, childDefaultStyle);
    forChild.recycle();

    return new ContextThemeWrapper(context, childStyleId);
}

From source file:com.facebook.react.views.toolbar.ReactToolbarManager.java

private static int[] getDefaultColors(Context context) {
    Resources.Theme theme = context.getTheme();
    TypedArray toolbarStyle = null;
    TypedArray textAppearances = null;// w ww.j  a  v  a2  s .  c  o  m
    TypedArray titleTextAppearance = null;
    TypedArray subtitleTextAppearance = null;

    try {
        toolbarStyle = theme.obtainStyledAttributes(new int[] { getIdentifier(context, "toolbarStyle") });

        int toolbarStyleResId = toolbarStyle.getResourceId(0, 0);
        textAppearances = theme.obtainStyledAttributes(toolbarStyleResId,
                new int[] { getIdentifier(context, "titleTextAppearance"),
                        getIdentifier(context, "subtitleTextAppearance"), });

        int titleTextAppearanceResId = textAppearances.getResourceId(0, 0);
        int subtitleTextAppearanceResId = textAppearances.getResourceId(1, 0);

        titleTextAppearance = theme.obtainStyledAttributes(titleTextAppearanceResId,
                new int[] { android.R.attr.textColor });
        subtitleTextAppearance = theme.obtainStyledAttributes(subtitleTextAppearanceResId,
                new int[] { android.R.attr.textColor });

        int titleTextColor = titleTextAppearance.getColor(0, Color.BLACK);
        int subtitleTextColor = subtitleTextAppearance.getColor(0, Color.BLACK);

        return new int[] { titleTextColor, subtitleTextColor };
    } finally {
        recycleQuietly(toolbarStyle);
        recycleQuietly(textAppearances);
        recycleQuietly(titleTextAppearance);
        recycleQuietly(subtitleTextAppearance);
    }
}

From source file:android.support.wear.widget.drawer.WearableDrawerView.java

private static Drawable getDrawable(Context context, TypedArray typedArray, @StyleableRes int index) {
    Drawable background;//from   w  ww .  ja v a 2s.  c o  m
    int backgroundResId = typedArray.getResourceId(index, 0);
    if (backgroundResId == 0) {
        background = typedArray.getDrawable(index);
    } else {
        background = context.getDrawable(backgroundResId);
    }
    return background;
}

From source file:com.hyc.skin.core.SkinViewInflater.java

/**
 * Allows us to emulate the {@code android:theme} attribute for devices before L.
 *///from  w  w w  . j ava 2 s  . co  m
private static Context themifyContext(Context context, AttributeSet attrs, boolean useAndroidTheme,
        boolean useAppTheme) {
    final TypedArray a = context.obtainStyledAttributes(attrs, android.support.v7.appcompat.R.styleable.View, 0,
            0);
    int themeId = 0;
    if (useAndroidTheme) {
        // First try reading android:theme if enabled
        themeId = a.getResourceId(android.support.v7.appcompat.R.styleable.View_android_theme, 0);
    }
    if (useAppTheme && themeId == 0) {
        // ...if that didn't work, try reading app:theme (for legacy reasons) if enabled
        themeId = a.getResourceId(android.support.v7.appcompat.R.styleable.View_theme, 0);

        if (themeId != 0) {
            Log.i(LOG_TAG, "app:theme is now deprecated. " + "Please move to using android:theme instead.");
        }
    }
    a.recycle();

    if (themeId != 0 && (!(context instanceof ContextThemeWrapper)
            || ((ContextThemeWrapper) context).getThemeResId() != themeId)) {
        // If the context isn't a ContextThemeWrapper, or it is but does not have
        // the same theme as we need, wrap it in a new wrapper
        context = new ContextThemeWrapper(context, themeId);
    }
    return context;
}

From source file:com.bilibili.magicasakura.utils.ThemeUtils.java

public static int getThemeAttrId(Context context, @AttrRes int attr) {
    TEMP_ARRAY[0] = attr;/* ww  w.jav  a2s . c  o  m*/
    TypedArray a = context.obtainStyledAttributes(null, TEMP_ARRAY);
    try {
        return a.getResourceId(0, 0);
    } finally {
        a.recycle();
    }
}

From source file:com.bilibili.magicasakura.utils.ThemeUtils.java

public static int getThemeAttrId(Context context, AttributeSet attrs, @AttrRes int attr) {
    TEMP_ARRAY[0] = attr;//from  www.  j  a  v a2s. c  o  m
    TypedArray a = context.obtainStyledAttributes(attrs, TEMP_ARRAY);
    try {
        return a.getResourceId(0, 0);
    } finally {
        a.recycle();
    }
}

From source file:com.facebook.litho.widget.TextSpec.java

@OnLoadStyle
static void onLoadStyle(ComponentContext c, Output<TruncateAt> ellipsize,
        Output<Boolean> shouldIncludeFontPadding, Output<Float> spacingMultiplier, Output<Integer> minLines,
        Output<Integer> maxLines, Output<Integer> minEms, Output<Integer> maxEms, Output<Integer> minWidth,
        Output<Integer> maxWidth, Output<Boolean> isSingleLine, Output<CharSequence> text,
        Output<ColorStateList> textColorStateList, Output<Integer> linkColor, Output<Integer> highlightColor,
        Output<Integer> textSize, Output<Alignment> textAlignment, Output<Integer> textStyle,
        Output<Float> shadowRadius, Output<Float> shadowDx, Output<Float> shadowDy, Output<Integer> shadowColor,
        Output<VerticalGravity> verticalGravity) {

    //check first if provided attributes contain textAppearance. As an analogy to TextView behavior,
    //we will parse textAppearance attributes first and then will override leftovers from main style
    TypedArray a = c.obtainStyledAttributes(R.styleable.Text_TextAppearanceAttr, 0);

    int textAppearanceResId = a.getResourceId(R.styleable.Text_TextAppearanceAttr_android_textAppearance, -1);
    a.recycle();/*from  ww w.j  a  va 2s . co m*/
    if (textAppearanceResId != -1) {
        a = c.getTheme().obtainStyledAttributes(textAppearanceResId, R.styleable.Text);
        resolveStyleAttrsForTypedArray(a, ellipsize, shouldIncludeFontPadding, spacingMultiplier, minLines,
                maxLines, minEms, maxEms, minWidth, maxWidth, isSingleLine, text, textColorStateList, linkColor,
                highlightColor, textSize, textAlignment, textStyle, shadowRadius, shadowDx, shadowDy,
                shadowColor, verticalGravity);
        a.recycle();
    }

    //now (after we parsed textAppearance) we can move on to main style attributes
    a = c.obtainStyledAttributes(R.styleable.Text, 0);
    resolveStyleAttrsForTypedArray(a, ellipsize, shouldIncludeFontPadding, spacingMultiplier, minLines,
            maxLines, minEms, maxEms, minWidth, maxWidth, isSingleLine, text, textColorStateList, linkColor,
            highlightColor, textSize, textAlignment, textStyle, shadowRadius, shadowDx, shadowDy, shadowColor,
            verticalGravity);

    a.recycle();
}

From source file:com.forrestguice.suntimeswidget.SuntimesUtils.java

public static ImageSpan createDstSpan(Context context, int width, int height) {
    TypedArray a = context.obtainStyledAttributes(new int[] { R.attr.icActionDst, R.attr.tagColor_dst });
    int drawableID = a.getResourceId(0, DEF_DST_DRAWABLE);
    int colorID = a.getResourceId(1, R.color.dstTag_dark);
    a.recycle();/*from  w w  w .ja va2s .  c  om*/
    return createImageSpan(context, drawableID, width, height, ContextCompat.getColor(context, colorID));
}

From source file:com.forrestguice.suntimeswidget.SuntimesUtils.java

public static ImageSpan createErrorSpan(Context context, int width, int height) {
    TypedArray a = context.obtainStyledAttributes(new int[] { R.attr.icActionError, R.attr.tagColor_error });
    int drawableID = a.getResourceId(0, DEF_ERROR_DRAWABLE);
    int colorID = a.getResourceId(1, R.color.errorTag_dark);
    a.recycle();//  ww w  .  j a  v a  2  s  . co m
    return createImageSpan(context, drawableID, width, height, ContextCompat.getColor(context, colorID));
}