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

/**
 * Tries to pull the Font Path from the Text Appearance.
 *
 * @param context     Activity Context/*  w  w  w.  j  ava 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) {
        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 www.  j ava  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 View Style as this is the next decendent after being
 * defined in the View's xml.//w ww  .  j  a  v a2 s  .  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, getAttributeArray(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//  w w w .j  a  v  a2  s  .  com
 * @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 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  ww  .ja v a 2  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
 * @param styleAttrId    Theme style id//  w  ww  .  ja va2 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.hitomi.basic.view.percentlayout.PercentLayoutHelper.java

private static PercentLayoutInfo.PercentVal getPercentVal(TypedArray array, int index, boolean baseWidth) {
    String sizeStr = array.getString(index);
    return getPercentVal(sizeStr, baseWidth);
}

From source file:android.percent.support.PercentLayoutHelper.java

private static PercentLayoutInfo.PercentVal getPercentVal(TypedArray array, int index, boolean baseWidth) {
    String sizeStr = array.getString(index);
    PercentLayoutInfo.PercentVal percentVal = getPercentVal(sizeStr, baseWidth);
    return percentVal;
}

From source file:com.spt.carengine.view.PercentLayoutHelper.java

/**
 * Constructs a PercentLayoutInfo from attributes associated with a View.
 * Call this method from {@code LayoutParams(Context c, AttributeSet attrs)}
 * constructor./*from w  ww  . j a  v a  2 s . c o m*/
 */
public static PercentLayoutInfo getPercentLayoutInfo(Context context, AttributeSet attrs) {
    PercentLayoutInfo info = null;
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PercentLayout_Layout);

    int index = R.styleable.PercentLayout_Layout_layout_widthPercent;
    String sizeStr = array.getString(index);
    PercentLayoutInfo.PercentVal percentVal = getPercentVal(sizeStr, true);
    if (percentVal != null) {
        info = checkForInfoExists(info);
        info.widthPercent = percentVal;
    }
    sizeStr = array.getString(R.styleable.PercentLayout_Layout_layout_heightPercent);
    percentVal = getPercentVal(sizeStr, false);

    if (sizeStr != null) {
        info = checkForInfoExists(info);
        info.heightPercent = percentVal;
    }

    // value =
    // array.getFraction(R.styleable.PercentLayout_Layout_layout_marginPercent,
    // 1, 1, -1f);
    sizeStr = array.getString(R.styleable.PercentLayout_Layout_layout_marginPercent);
    // just for judge
    percentVal = getPercentVal(sizeStr, false);

    if (percentVal != null) {
        info = checkForInfoExists(info);
        info.leftMarginPercent = getPercentVal(sizeStr, true);
        info.topMarginPercent = getPercentVal(sizeStr, false);
        info.rightMarginPercent = getPercentVal(sizeStr, true);
        info.bottomMarginPercent = getPercentVal(sizeStr, false);
    }
    // value =
    // array.getFraction(R.styleable.PercentLayout_Layout_layout_marginLeftPercent,
    // 1, 1,
    // -1f);
    sizeStr = array.getString(R.styleable.PercentLayout_Layout_layout_marginLeftPercent);
    percentVal = getPercentVal(sizeStr, true);
    if (percentVal != null) {
        info = checkForInfoExists(info);
        info.leftMarginPercent = percentVal;
    }

    // value =
    // array.getFraction(R.styleable.PercentLayout_Layout_layout_marginTopPercent,
    // 1, 1,
    // -1f);
    sizeStr = array.getString(R.styleable.PercentLayout_Layout_layout_marginTopPercent);
    percentVal = getPercentVal(sizeStr, false);
    if (percentVal != null) {
        info = checkForInfoExists(info);
        info.topMarginPercent = percentVal;
    }
    // value =
    // array.getFraction(R.styleable.PercentLayout_Layout_layout_marginRightPercent,
    // 1, 1,
    // -1f);
    sizeStr = array.getString(R.styleable.PercentLayout_Layout_layout_marginRightPercent);
    percentVal = getPercentVal(sizeStr, true);
    if (percentVal != null) {
        info = checkForInfoExists(info);
        info.rightMarginPercent = percentVal;
    }
    // value =
    // array.getFraction(R.styleable.PercentLayout_Layout_layout_marginBottomPercent,
    // 1, 1,
    // -1f);
    sizeStr = array.getString(R.styleable.PercentLayout_Layout_layout_marginBottomPercent);
    percentVal = getPercentVal(sizeStr, false);
    if (percentVal != null) {
        info = checkForInfoExists(info);
        info.bottomMarginPercent = percentVal;
    }
    // value =
    // array.getFraction(R.styleable.PercentLayout_Layout_layout_marginStartPercent,
    // 1, 1,
    // -1f);
    sizeStr = array.getString(R.styleable.PercentLayout_Layout_layout_marginStartPercent);
    percentVal = getPercentVal(sizeStr, true);
    if (percentVal != null) {
        info = checkForInfoExists(info);
        info.startMarginPercent = percentVal;
    }
    // value =
    // array.getFraction(R.styleable.PercentLayout_Layout_layout_marginEndPercent,
    // 1, 1,
    // -1f);
    sizeStr = array.getString(R.styleable.PercentLayout_Layout_layout_marginEndPercent);
    percentVal = getPercentVal(sizeStr, true);
    if (percentVal != null) {
        info = checkForInfoExists(info);
        info.endMarginPercent = percentVal;
    }

    // textSizePercent
    sizeStr = array.getString(R.styleable.PercentLayout_Layout_layout_textSizePercent);
    percentVal = getPercentVal(sizeStr, false);
    if (percentVal != null) {
        info = checkForInfoExists(info);
        info.textSizePercent = percentVal;
    }

    // maxWidth
    percentVal = getPercentVal(array, R.styleable.PercentLayout_Layout_layout_maxWidthPercent, true);
    if (percentVal != null) {
        checkForInfoExists(info);
        info.maxWidthPercent = percentVal;
    }
    // maxHeight
    percentVal = getPercentVal(array, R.styleable.PercentLayout_Layout_layout_maxHeightPercent, false);
    if (percentVal != null) {
        checkForInfoExists(info);
        info.maxHeightPercent = percentVal;
    }
    // minWidth
    percentVal = getPercentVal(array, R.styleable.PercentLayout_Layout_layout_minWidthPercent, true);
    if (percentVal != null) {
        checkForInfoExists(info);
        info.minWidthPercent = percentVal;
    }
    // minHeight
    percentVal = getPercentVal(array, R.styleable.PercentLayout_Layout_layout_minHeightPercent, false);
    if (percentVal != null) {
        checkForInfoExists(info);
        info.minHeightPercent = percentVal;
    }

    array.recycle();
    return info;
}

From source file:de.mrapp.android.util.ThemeUtil.java

/**
 * Obtains the string, which corresponds to a specific resource id, from a specific theme. If
 * the given resource id is invalid, a {@link NotFoundException} will be thrown.
 *
 * @param context/*from   www  .ja  va  2  s  .  co  m*/
 *         The context, which should be used, as an instance of the class {@link Context}. The
 *         context may not be null
 * @param themeResourceId
 *         The resource id of the theme, the attribute should be obtained from, as an {@link
 *         Integer} value or -1, if the attribute should be obtained from the given context's
 *         theme
 * @param resourceId
 *         The resource id of the attribute, which should be obtained, as an {@link Integer}
 *         value. The resource id must corresponds to a valid theme attribute
 * @return The string, which has been obtained, as a {@link String}
 */
public static String getString(@NonNull final Context context, @StyleRes final int themeResourceId,
        @AttrRes final int resourceId) {
    ensureNotNull(context, "The context may not be null");
    TypedArray typedArray = null;

    try {
        typedArray = obtainStyledAttributes(context, themeResourceId, resourceId);
        String string = typedArray.getString(0);

        if (string == null) {
            throw new NotFoundException("Resource ID #0x" + Integer.toHexString(resourceId) + " is not valid");
        }

        return string;
    } finally {
        if (typedArray != null) {
            typedArray.recycle();
        }
    }
}