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

/**
 *//*from   ww w  . j a  v  a  2 s. c o m*/
public static int[] getDrawableIdsArray(Context context, @ArrayRes int drawableArraysId) {
    TypedArray ta = context.getResources().obtainTypedArray(drawableArraysId);
    int count = ta.length();
    int[] ids = new int[count];
    for (int i = 0; i < count; i++) {
        ids[i] = ta.getResourceId(i, 0);
    }
    ta.recycle();
    return ids;
}

From source file:Main.java

public static int[] getIdArray(Context context, int arrayId) {
    TypedArray typedArray = context.getResources().obtainTypedArray(arrayId);
    int length = typedArray.length();
    int[] ids = new int[length];
    for (int i = 0; i < length; i++) {
        ids[i] = typedArray.getResourceId(i, -1);
    }//  www .  java  2 s.c  o  m
    typedArray.recycle();
    return ids;
}

From source file:com.carlrice.reader.utils.UiUtils.java

static public int getAttrResource(Context context, int attrId, int defValue) {
    TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { attrId });
    int result = a.getResourceId(0, defValue);
    a.recycle();//w w  w  . j av  a2 s.  com
    return result;
}

From source file:Main.java

public static int[] getEmojiArray(Context context, int arryId) {

    final TypedArray ar = context.getResources().obtainTypedArray(arryId);
    final int length = ar.length();
    final int[] array = new int[length];
    for (int i = 0; i < length; ++i) {
        array[i] = ar.getResourceId(i, 0);
    }/*from   w  ww  . j  ava2s  .co m*/
    return array;
}

From source file:Main.java

public static int[] getResourceIds(Context c, @ArrayRes int array) {
    final TypedArray typedArray = c.getResources().obtainTypedArray(array);
    final int[] resourceIds = new int[typedArray.length()];
    for (int i = 0; i < typedArray.length(); i++) {
        resourceIds[i] = typedArray.getResourceId(i, 0);
    }//from   ww  w  .ja va 2 s  .  co m
    typedArray.recycle();
    return resourceIds;
}

From source file:com.ichi2.themes.Themes.java

public static int[] getResFromAttr(Context context, int[] attrs) {
    TypedArray ta = context.obtainStyledAttributes(attrs);
    for (int i = 0; i < attrs.length; i++) {
        attrs[i] = ta.getResourceId(i, 0);
    }/*from w w  w .  j a v a2  s  .c om*/
    ta.recycle();
    return attrs;
}

From source file:Main.java

/**
 * Tries to pull the Font Path from the Text Appearance.
 *
 * @param context     Activity Context/*  w  ww. ja  v  a 2s. 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 == null || attrs == null) {
        return null;
    }

    int textAppearanceId = -1;
    final TypedArray typedArrayAttr = context.obtainStyledAttributes(attrs, ANDROID_ATTR_TEXT_APPEARANCE);
    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, 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

@SuppressLint("UseSparseArrays")
@SuppressWarnings("ResourceType")
public static Map<Integer, Pair<String, String>> obtainBadgeMap(Context context, @ArrayRes int id) {

    TypedArray badgeArray = context.getResources().obtainTypedArray(id);

    Map<Integer, Pair<String, String>> badgeMap = new HashMap<>();
    for (int i = 0; i < badgeArray.length(); i++) {
        int resId = badgeArray.getResourceId(i, -1);
        if (resId != -1) {
            TypedArray array = context.getResources().obtainTypedArray(resId);
            badgeMap.put(resId, new Pair<>(array.getString(0), array.getString(1)));
            array.recycle();//from   w ww  .  j  ava  2 s. c  o m
        }
    }
    badgeArray.recycle();
    return badgeMap;
}

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// w w w. ja v a2 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 == null)
        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,
            new int[] { 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, 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: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 ww. jav a2 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,
            new int[] { 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, new int[] { attributeId });
    if (subTypedArray != null) {
        try {
            return subTypedArray.getString(0);
        } catch (Exception ignore) {
            // Failed for some reason.
            return null;
        } finally {
            subTypedArray.recycle();
        }
    }
    return null;
}