Example usage for android.content Context obtainStyledAttributes

List of usage examples for android.content Context obtainStyledAttributes

Introduction

In this page you can find the example usage for android.content Context obtainStyledAttributes.

Prototype

public final TypedArray obtainStyledAttributes(AttributeSet set, @StyleableRes int[] attrs) 

Source Link

Document

Retrieve styled attribute information in this Context's theme.

Usage

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();/*from w  ww  .j  av a 2  s . c  o  m*/
        if (color != 0) {
            return color;
        }
    }
    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

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

/**
 * Tries to pull the Font Path from the Text Appearance.
 *
 * @param context     Activity Context//  www.j a 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 == -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:android.support.v7.widget.TintTypedArray.java

public static TintTypedArray obtainStyledAttributes(Context context, int resid, int[] attrs) {
    return new TintTypedArray(context, context.obtainStyledAttributes(resid, attrs));
}

From source file:android.support.v7.widget.TintTypedArray.java

public static TintTypedArray obtainStyledAttributes(Context context, AttributeSet set, int[] attrs) {
    return new TintTypedArray(context, context.obtainStyledAttributes(set, attrs));
}

From source file:Main.java

static public int getAccentColor(Context context) {
    int identifier = context.getResources().getIdentifier("colorAccent", "attr", context.getPackageName());
    if (identifier == 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        identifier = android.R.attr.colorAccent;
    }/*from   w  ww  .ja  va  2  s. c o  m*/
    if (identifier > 0) {
        TypedValue typedValue = new TypedValue();
        TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[] { identifier });
        if (a != null) {
            int color = a.getColor(0, 0);
            a.recycle();
            return color;
        }
    }
    return Color.BLACK;
}

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();//from w  w w .  j  av  a  2s . co m

    return dialogTheme;
}

From source file:carbon.internal.PercentLayoutHelper.java

/**
 * Constructs a PercentLayoutInfo from attributes associated with a View. Call this method from
 * {@code LayoutParams(Context c, AttributeSet attrs)} constructor.
 *///  w  ww  . jav  a2  s .  co  m
public static PercentLayoutInfo getPercentLayoutInfo(Context context, AttributeSet attrs) {
    PercentLayoutInfo info = null;
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Carbon);
    float value = array.getFraction(R.styleable.Carbon_carbon_widthPercent, 1, 1, -1f);
    if (value != -1f) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "percent width: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.widthPercent = value;
    }
    value = array.getFraction(R.styleable.Carbon_carbon_heightPercent, 1, 1, -1f);
    if (value != -1f) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "percent height: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.heightPercent = value;
    }
    value = array.getFraction(R.styleable.Carbon_carbon_marginPercent, 1, 1, -1f);
    if (value != -1f) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "percent margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.leftMarginPercent = value;
        info.topMarginPercent = value;
        info.rightMarginPercent = value;
        info.bottomMarginPercent = value;
    }
    value = array.getFraction(R.styleable.Carbon_carbon_marginLeftPercent, 1, 1, -1f);
    if (value != -1f) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "percent left margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.leftMarginPercent = value;
    }
    value = array.getFraction(R.styleable.Carbon_carbon_marginTopPercent, 1, 1, -1f);
    if (value != -1f) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "percent top margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.topMarginPercent = value;
    }
    value = array.getFraction(R.styleable.Carbon_carbon_marginRightPercent, 1, 1, -1f);
    if (value != -1f) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "percent right margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.rightMarginPercent = value;
    }
    value = array.getFraction(R.styleable.Carbon_carbon_marginBottomPercent, 1, 1, -1f);
    if (value != -1f) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "percent bottom margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.bottomMarginPercent = value;
    }
    value = array.getFraction(R.styleable.Carbon_carbon_marginStartPercent, 1, 1, -1f);
    if (value != -1f) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "percent start margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.startMarginPercent = value;
    }
    value = array.getFraction(R.styleable.Carbon_carbon_marginEndPercent, 1, 1, -1f);
    if (value != -1f) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "percent end margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.endMarginPercent = value;
    }

    value = array.getFraction(R.styleable.Carbon_carbon_aspectRatio, 1, 1, -1f);
    if (value != -1f) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "aspect ratio: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.aspectRatio = value;
    }

    array.recycle();
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "constructed: " + info);
    }
    return info;
}

From source file:android.support.percent.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  av a2 s . com*/
public static PercentLayoutInfo getPercentLayoutInfo(Context context, AttributeSet attrs) {
    PercentLayoutInfo info = null;
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PercentLayout_Layout);
    float value = array.getFraction(R.styleable.PercentLayout_Layout_layout_widthPercent, 1, 1, -1f);
    if (value != -1f) {
        if (VERBOSE) {
            Log.v(TAG, "percent width: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.widthPercent = value;
    }
    value = array.getFraction(R.styleable.PercentLayout_Layout_layout_heightPercent, 1, 1, -1f);
    if (value != -1f) {
        if (VERBOSE) {
            Log.v(TAG, "percent height: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.heightPercent = value;
    }
    value = array.getFraction(R.styleable.PercentLayout_Layout_layout_marginPercent, 1, 1, -1f);
    if (value != -1f) {
        if (VERBOSE) {
            Log.v(TAG, "percent margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.leftMarginPercent = value;
        info.topMarginPercent = value;
        info.rightMarginPercent = value;
        info.bottomMarginPercent = value;
    }
    value = array.getFraction(R.styleable.PercentLayout_Layout_layout_marginLeftPercent, 1, 1, -1f);
    if (value != -1f) {
        if (VERBOSE) {
            Log.v(TAG, "percent left margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.leftMarginPercent = value;
    }
    value = array.getFraction(R.styleable.PercentLayout_Layout_layout_marginTopPercent, 1, 1, -1f);
    if (value != -1f) {
        if (VERBOSE) {
            Log.v(TAG, "percent top margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.topMarginPercent = value;
    }
    value = array.getFraction(R.styleable.PercentLayout_Layout_layout_marginRightPercent, 1, 1, -1f);
    if (value != -1f) {
        if (VERBOSE) {
            Log.v(TAG, "percent right margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.rightMarginPercent = value;
    }
    value = array.getFraction(R.styleable.PercentLayout_Layout_layout_marginBottomPercent, 1, 1, -1f);
    if (value != -1f) {
        if (VERBOSE) {
            Log.v(TAG, "percent bottom margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.bottomMarginPercent = value;
    }
    value = array.getFraction(R.styleable.PercentLayout_Layout_layout_marginStartPercent, 1, 1, -1f);
    if (value != -1f) {
        if (VERBOSE) {
            Log.v(TAG, "percent start margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.startMarginPercent = value;
    }
    value = array.getFraction(R.styleable.PercentLayout_Layout_layout_marginEndPercent, 1, 1, -1f);
    if (value != -1f) {
        if (VERBOSE) {
            Log.v(TAG, "percent end margin: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.endMarginPercent = value;
    }

    value = array.getFraction(R.styleable.PercentLayout_Layout_layout_aspectRatio, 1, 1, -1f);
    if (value != -1f) {
        if (VERBOSE) {
            Log.v(TAG, "aspect ratio: " + value);
        }
        info = info != null ? info : new PercentLayoutInfo();
        info.aspectRatio = value;
    }

    array.recycle();
    if (DEBUG) {
        Log.d(TAG, "constructed: " + info);
    }
    return info;
}