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:Main.java

/**
 * Tries to pull the Font Path from the Text Appearance.
 *
 * @param context     Activity Context/*ww  w.j  a v a  2  s. c o m*/
 * @param attrs       View Attributes
 * @param attributeId if -1 returns null.
 * @return returns null if attribute is not defined or if no TextAppearance is found.
 */
static String pullFontPathFromTextAppearance(final Context context, AttributeSet attrs, int attributeId) {
    if (attributeId == -1 || attrs == null) {
        return null;
    }

    int textAppearanceId = -1;
    final TypedArray typedArrayAttr = context.obtainStyledAttributes(attrs,
            new int[] { android.R.attr.textAppearance });
    if (typedArrayAttr != null) {
        try {
            textAppearanceId = typedArrayAttr.getResourceId(0, -1);
        } catch (Exception ignored) {
            // Failed for some reason
            return null;
        } finally {
            typedArrayAttr.recycle();
        }
    }

    final TypedArray textAppearanceAttrs = context.obtainStyledAttributes(textAppearanceId,
            new int[] { attributeId });
    if (textAppearanceAttrs != null) {
        try {
            return textAppearanceAttrs.getString(0);
        } catch (Exception ignore) {
            // Failed for some reason.
            return null;
        } finally {
            textAppearanceAttrs.recycle();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Tries to pull the Font Path from the Text Appearance.
 *
 * @param context     Activity Context//ww w  .  java2s.c  o  m
 * @param attrs       View Attributes
 * @param attributeId if -1 returns null.
 * @return returns null if attribute is not defined or if no TextAppearance is found.
 */
static String pullFontPathFromTextAppearance(final Context context, AttributeSet attrs, int attributeId) {
    if (attributeId == -1) {
        return null;
    }

    int textAppearanceId = -1;
    final TypedArray typedArrayAttr = context.obtainStyledAttributes(attrs,
            new int[] { android.R.attr.textAppearance });
    if (typedArrayAttr != null) {
        try {
            textAppearanceId = typedArrayAttr.getResourceId(0, -1);
        } catch (Exception ignored) {
            // Failed for some reason
            return null;
        } finally {
            typedArrayAttr.recycle();
        }
    }

    final TypedArray textAppearanceAttrs = context.obtainStyledAttributes(textAppearanceId,
            new int[] { attributeId });
    if (textAppearanceAttrs != null) {
        try {
            return textAppearanceAttrs.getString(0);
        } catch (Exception ignore) {
            // Failed for some reason.
            return null;
        } finally {
            textAppearanceAttrs.recycle();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Last but not least, try to pull the Font Path from the Theme, which is defined.
 *
 * @param context        Activity Context
 * @param styleAttrId    Theme style id/*from w w w .ja  va 2 s .  c om*/
 * @param subStyleAttrId the sub style from the theme to look up after the first style
 * @param attributeId    if -1 returns null.
 * @return null if no theme or attribute defined.
 */
static String pullFontPathFromTheme(Context context, int styleAttrId, int subStyleAttrId, int attributeId) {
    if (styleAttrId == -1 || attributeId == -1)
        return null;

    final Resources.Theme theme = context.getTheme();
    final TypedValue value = new TypedValue();

    theme.resolveAttribute(styleAttrId, value, true);
    int subStyleResId = -1;
    final TypedArray parentTypedArray = theme.obtainStyledAttributes(value.resourceId,
            getAttributeArray(subStyleAttrId));
    try {
        subStyleResId = parentTypedArray.getResourceId(0, -1);
    } catch (Exception ignore) {
        // Failed for some reason.
        return null;
    } finally {
        parentTypedArray.recycle();
    }

    if (subStyleResId == -1)
        return null;
    final TypedArray subTypedArray = context.obtainStyledAttributes(subStyleResId,
            getAttributeArray(attributeId));
    if (subTypedArray != null) {
        try {
            return subTypedArray.getString(0);
        } catch (Exception ignore) {
            // Failed for some reason.
            return null;
        } finally {
            subTypedArray.recycle();
        }
    }
    return null;
}

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

static ColorStateList inflateColorStateList(Context context, XmlPullParser parser, AttributeSet attrs)
        throws IOException, XmlPullParserException {
    final int innerDepth = parser.getDepth() + 1;
    int depth;/*from w w  w. j a v  a 2 s .  c  o m*/
    int type;

    LinkedList<int[]> stateList = new LinkedList<>();
    LinkedList<Integer> colorList = new LinkedList<>();

    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG || depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }

        TypedArray a1 = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.color });
        final int value = a1.getResourceId(0, Color.MAGENTA);
        final int baseColor = value == Color.MAGENTA ? Color.MAGENTA
                : ThemeUtils.replaceColorById(context, value);
        a1.recycle();
        TypedArray a2 = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.alpha });
        final float alphaMod = a2.getFloat(0, 1.0f);
        a2.recycle();
        colorList.add(alphaMod != 1.0f
                ? ColorUtils.setAlphaComponent(baseColor, Math.round(Color.alpha(baseColor) * alphaMod))
                : baseColor);

        stateList.add(extractStateSet(attrs));
    }

    if (stateList.size() > 0 && stateList.size() == colorList.size()) {
        int[] colors = new int[colorList.size()];
        for (int i = 0; i < colorList.size(); i++) {
            colors[i] = colorList.get(i);
        }
        return new ColorStateList(stateList.toArray(new int[stateList.size()][]), colors);
    }
    return null;
}

From source file:Main.java

/**
 * Tries to pull the Font Path from the Text Appearance.
 *
 * @param context     Activity Context/*from  ww  w .j a  v a2 s .c  om*/
 * @param attrs       View Attributes
 * @param attributeId if -1 returns null.
 * @return returns null if attribute is not defined or if no TextAppearance is found.
 */
static String pullFontPathFromTextAppearance(final Context context, AttributeSet attrs, int attributeId) {
    if (attributeId == -1 || attrs == null) {
        return null;
    }

    int textAppearanceId = -1;
    final TypedArray typedArrayAttr = context.obtainStyledAttributes(attrs,
            getAttributeArray(android.R.attr.textAppearance));
    if (typedArrayAttr != null) {
        try {
            textAppearanceId = typedArrayAttr.getResourceId(0, -1);
        } catch (Exception ignored) {
            // Failed for some reason
            return null;
        } finally {
            typedArrayAttr.recycle();
        }
    }

    final TypedArray textAppearanceAttrs = context.obtainStyledAttributes(textAppearanceId,
            getAttributeArray(attributeId));
    if (textAppearanceAttrs != null) {
        try {
            return textAppearanceAttrs.getString(0);
        } catch (Exception ignore) {
            // Failed for some reason.
            return null;
        } finally {
            textAppearanceAttrs.recycle();
        }
    }
    return null;
}

From source file:com.gelakinetic.mtgfam.helpers.ImageGetterHelper.java

/**
 * This helper function translates an attribute into a resource ID
 *
 * @param attr The attribute ID//from  www. j a  va 2 s . c o m
 * @return the resource ID
 */
private static int getResourceIdFromAttr(Resources.Theme theme, int attr) {
    TypedArray ta = theme.obtainStyledAttributes(new int[] { attr });
    assert ta != null;
    int resId = ta.getResourceId(0, 0);
    ta.recycle();
    return resId;
}

From source file:android.support.v7.internal.widget.ViewUtils.java

/**
 * Allows us to emulate the {@code android:theme} attribute for devices before L.
 *//*from   w ww .ja  va 2  s .  co m*/
public 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, 0);

        if (themeId != 0) {
            Log.i(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 ContextThemeWrapperCompat, 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.nextgis.maplibui.util.ControlHelper.java

public static int getDialogTheme(Context context, int theme) {
    int dialogTheme = R.style.Theme_NextGIS_AppCompat_Light_Dialog;
    int[] attrs = { android.R.attr.alertDialogStyle };
    TypedArray ta = context.obtainStyledAttributes(theme, attrs);
    dialogTheme = ta.getResourceId(0, dialogTheme);
    ta.recycle();/* w  w  w . j  a va  2  s . c  o m*/

    return dialogTheme;
}

From source file:ca.marklauman.dominionpicker.CardAdapter.java

/** Retrieve an array of drawable resources from
 *  the xml of the provided {@link Context}.
 *  @param c The {@code Context} to search for the array.
 *  @param resourceId The resource id of an
 *  {@code <array>} containing a list of drawables.
 *  @return The resource ids of all the drawables
 *  in the array, in the order in which they appear
 *  in the xml.                                  */
public static int[] getDrawables(Context c, int resourceId) {
    TypedArray ta = c.getResources().obtainTypedArray(resourceId);
    if (ta == null)
        return null;

    int[] res = new int[ta.length()];
    for (int i = 0; i < ta.length(); i++)
        res[i] = ta.getResourceId(i, -1);

    ta.recycle();//w w  w.ja v a  2s .com
    return res;
}

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

/**
 * Allows us to emulate the {@code android:theme} attribute for devices before L.
 *//*from  w  w w.j  av a 2  s.com*/
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, 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;
}