Example usage for android.content.res TypedArray recycle

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

Introduction

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

Prototype

public void recycle() 

Source Link

Document

Recycles the TypedArray, to be re-used by a later caller.

Usage

From source file:Main.java

/**
 *//*from   w  ww .  j av  a  2s  . c om*/
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:android.support.v7.app.MediaRouterThemeHelper.java

private static int getThemeColor(Context context, int style, int attr) {
    if (style != 0) {
        int[] attrs = { attr };
        TypedArray ta = context.obtainStyledAttributes(style, attrs);
        int color = ta.getColor(0, 0);
        ta.recycle();
        if (color != 0) {
            return color;
        }//  w  w w.  j a  v a2s. c  om
    }
    TypedValue value = new TypedValue();
    context.getTheme().resolveAttribute(attr, value, true);
    if (value.resourceId != 0) {
        return context.getResources().getColor(value.resourceId);
    }
    return value.data;
}

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);
    }/*from   ww  w  .  j a v a  2s .c o m*/
    typedArray.recycle();
    return ids;
}

From source file:Main.java

public static Drawable resolveDrawable(Context context, int attr, Drawable fallback) {
    TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { attr });
    try {//from  w  w w.java 2 s . com
        Drawable d = a.getDrawable(0);
        if (d == null && fallback != null)
            d = fallback;
        return d;
    } finally {
        a.recycle();
    }
}

From source file:Main.java

private static Drawable resolveDrawable(Context context, @AttrRes int attr,
        @SuppressWarnings("SameParameterValue") Drawable fallback) {
    TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { attr });
    try {/*w w w . j a  va 2 s .c o  m*/
        Drawable d = a.getDrawable(0);
        if (d == null && fallback != null)
            d = fallback;
        return d;
    } finally {
        a.recycle();
    }
}

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 .  j  av  a  2 s.c  o  m
    typedArray.recycle();
    return resourceIds;
}

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//  ww  w  . j  a  va2s.c  o  m
 * @param styleId     Theme style id
 * @param attributeId if -1 returns null.
 * @return null if no theme or attribute defined.
 */
static String pullFontPathFromTheme(Context context, int styleId, int attributeId) {
    if (styleId == -1 || attributeId == -1)
        return null;

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

    theme.resolveAttribute(styleId, value, true);
    final TypedArray typedArray = theme.obtainStyledAttributes(value.resourceId, new int[] { attributeId });
    try {
        return typedArray.getString(0);
    } catch (Exception ignore) {
        // Failed for some reason.
        return null;
    } finally {
        typedArray.recycle();
    }
}

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/*from  w ww . ja v a 2 s .  c o  m*/
 * @param styleAttrId Theme style id
 * @param attributeId if -1 returns null.
 * @return null if no theme or attribute defined.
 */
static String pullFontPathFromTheme(Context context, int styleAttrId, 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);
    final TypedArray typedArray = theme.obtainStyledAttributes(value.resourceId, attributeId);
    try {
        String font = typedArray.getString(0);
        return font;
    } catch (Exception ignore) {
        // Failed for some reason.
        return null;
    } finally {
        typedArray.recycle();
    }
}

From source file:Main.java

public static ArrayList<String> loadTargetsDescriptions(Context ctxt, int resourceId) {
    TypedArray array = ctxt.getResources().obtainTypedArray(resourceId);
    final int count = array.length();
    ArrayList<String> targetContentDescriptions = new ArrayList<String>(count);
    for (int i = 0; i < count; i++) {
        String contentDescription = array.getString(i);
        targetContentDescriptions.add(contentDescription);
    }/*from w  w  w  .ja  v  a2 s  . co  m*/
    array.recycle();
    return targetContentDescriptions;
}

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/*from  w ww  .j  a  v a 2 s  .c o m*/
 * @param styleAttrId Theme style id
 * @param attributeId if -1 returns null.
 * @return null if no theme or attribute defined.
 */
static String pullFontPathFromTheme(Context context, int styleAttrId, 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);
    final TypedArray typedArray = theme.obtainStyledAttributes(value.resourceId, new int[] { attributeId });
    try {
        String font = typedArray.getString(0);
        return font;
    } catch (Exception ignore) {
        // Failed for some reason.
        return null;
    } finally {
        typedArray.recycle();
    }
}