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

/**
 * 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  .  j  a v a  2 s .c o m
 * @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 w  w . j  av a 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,
            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;
}

From source file:Main.java

/**
 * Tries to pull the Font Path from the Text Appearance.
 *
 * @param context     Activity Context// w  w  w . j av  a2s.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 == 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

/**
 * Last but not least, try to pull the Font Path from the Theme, which is defined.
 *
 * @param context     Activity Context// w  w w  .  j a  v  a2  s.co 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,
            getAttributeArray(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

/**
 * 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. j av a 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:Main.java

/**
 * Tries to pull the Font Path from the Text Appearance.
 *
 * @param context     Activity Context/*from www .  j  a v a  2s  .  co  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/*w  w  w  .  j av  a 2 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) {
        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: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();

    return dialogTheme;
}

From source file:Main.java

/**
 * Tries to pull the Font Path from the Text Appearance.
 *
 * @param context     Activity Context/* ww w .  ja  va 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 == -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:Main.java

public static int getColorAttribute(Context context, int key, int defaultColor) {
    if (sColorAttrsArray.get(key) != null) {
        return sColorAttrsArray.get(key);
    }/* w  w w .j ava 2  s .  c o m*/
    TypedArray a = null;
    try {
        int[] attr = new int[] { key };
        int indexOfAttrBackgroundColor = 0;
        a = context.obtainStyledAttributes(attr);
        sColorAttrsArray.put(key, a.getColor(indexOfAttrBackgroundColor, defaultColor));
    } catch (Exception e) {
        sColorAttrsArray.put(key, defaultColor);
        if (DEBUG)
            Log.w(TAG, "unexpected exception", e);
    } finally {
        if (a != null)
            a.recycle();
    }
    return sColorAttrsArray.get(key);
}