Example usage for android.content.res TypedArray getString

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

Introduction

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

Prototype

@Nullable
public String getString(@StyleableRes int index) 

Source Link

Document

Retrieves the string value for the attribute at index.

Usage

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 a  v  a2s .  com*/
        }
    }
    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//w w w  . j a v a  2 s.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   ww w  . j  av a  2s.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

/**
 * Last but not least, try to pull the Font Path from the Theme, which is defined.
 *
 * @param context     Activity Context//  ww  w . j  a va2  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, new int[] { 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

/**
 * Tries to pull the Font Path from the Text Appearance.
 *
 * @param context     Activity Context/*from  w  w 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 == 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// ww  w  .j a v a 2  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//  ww  w.j a v a 2s  .  co  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

/**
 * Tries to pull the Font Path from the View Style as this is the next decendent after being
 * defined in the View's xml./*from   www.jav  a 2s. co m*/
 *
 * @param context     Activity Activity Context
 * @param attrs       View Attributes
 * @param attributeId if -1 returns null.
 * @return null if attribute is not defined or found in the Style
 */
static String pullFontPathFromStyle(Context context, AttributeSet attrs, int attributeId) {
    if (attributeId == -1)
        return null;
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, new int[] { attributeId });
    if (typedArray != null) {
        try {
            // First defined attribute
            String fontFromAttribute = typedArray.getString(0);
            if (!TextUtils.isEmpty(fontFromAttribute)) {
                return fontFromAttribute;
            }
        } catch (Exception ignore) {
            // Failed for some reason.
        } finally {
            typedArray.recycle();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Tries to pull the Font Path from the View Style as this is the next decendent after being
 * defined in the View's xml.//w ww  .j  a  v  a2s . c o m
 *
 * @param context     Activity Activity Context
 * @param attrs       View Attributes
 * @param attributeId if -1 returns null.
 * @return null if attribute is not defined or found in the Style
 */
static String pullFontPathFromStyle(Context context, AttributeSet attrs, int attributeId) {
    if (attributeId == -1 || attrs == null)
        return null;
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, new int[] { attributeId });
    if (typedArray != null) {
        try {
            // First defined attribute
            String fontFromAttribute = typedArray.getString(0);
            if (!TextUtils.isEmpty(fontFromAttribute)) {
                return fontFromAttribute;
            }
        } catch (Exception ignore) {
            // Failed for some reason.
        } finally {
            typedArray.recycle();
        }
    }
    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 av  a  2 s  .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;
}