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(@StyleableRes int[] attrs) 

Source Link

Document

Retrieve styled attribute information in this Context's theme.

Usage

From source file:Main.java

public static int resolveResource(Context context, int attr) {
    int[] attribute = new int[] { attr };
    TypedArray array = context.obtainStyledAttributes(attribute);
    int reference = array.getResourceId(0, 0);
    array.recycle();/*from w  w  w .  j  a v a  2  s .  c o m*/
    return reference;
}

From source file:Main.java

/**
 * Gets an integer from an attribute or uses a default value if not present.
 * @param context context//ww  w.ja  v a 2  s. co  m
 * @param attrResId resource ID of the attribute
 * @param defaultValue fallback value
 * @return integer
 */
public static int getIntegerFromAttribute(Context context, @AttrRes int attrResId, int defaultValue) {
    int[] attrs = new int[] { attrResId };
    TypedArray ta = context.obtainStyledAttributes(attrs);
    final int value = ta.getInteger(0, defaultValue);
    ta.recycle();

    return value;
}

From source file:Main.java

public static int getActionBarHeight(Context context) {

    if (mActionBarHeight != -1) {
        return mActionBarHeight;
    }/*from  w w  w .  ja  v  a2 s .com*/

    TypedArray typedArray = context.obtainStyledAttributes(new int[] { android.R.attr.actionBarSize });
    mActionBarHeight = (int) typedArray.getDimension(0, 0);
    typedArray.recycle();

    return mActionBarHeight;
}

From source file:Main.java

/**
 * //  w ww .j  av  a  2 s  . c o m
 * @param context
 * @param view
 * @param resid
 * @return
 */
public static View addBackgroundIndicator(Context context, View view, int resid) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        TypedArray attributes = context.obtainStyledAttributes(new int[] { resid });
        int resource = attributes.getResourceId(0, 0);
        attributes.recycle();

        // setBackgroundResource resets padding
        int paddingLeft = view.getPaddingLeft();
        int paddingTop = view.getPaddingTop();
        int paddingRight = view.getPaddingRight();
        int paddingBottom = view.getPaddingBottom();
        view.setBackgroundResource(resource);
        view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
    }
    return view;
}

From source file:Main.java

public static int getColorAttribute(Context context, int key, int defaultColor) {
    if (sColorAttrsArray.get(key) != null) {
        return sColorAttrsArray.get(key);
    }/*from  w  w  w  .  ja va2 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);
}

From source file:com.appeaser.sublimepickerlibrary.utilities.SUtils.java

public static void initializeResources(Context context) {
    TypedArray a = context.obtainStyledAttributes(new int[] { R.attr.colorAccent, R.attr.colorControlHighlight,
            R.attr.colorControlActivated, R.attr.colorButtonNormal, android.R.attr.textColorPrimary,
            android.R.attr.textColorPrimaryInverse, R.attr.colorPrimary, R.attr.colorPrimaryDark,
            android.R.attr.textColorSecondary, android.R.attr.colorBackground,
            android.R.attr.textColorSecondaryInverse });

    if (a.hasValue(0))
        COLOR_ACCENT = a.getColor(0, Color.TRANSPARENT);

    if (a.hasValue(1))
        COLOR_CONTROL_HIGHLIGHT = a.getColor(1, Color.TRANSPARENT);

    if (a.hasValue(2))
        COLOR_CONTROL_ACTIVATED = a.getColor(2, Color.TRANSPARENT);

    if (a.hasValue(3))
        COLOR_BUTTON_NORMAL = a.getColor(3, Color.TRANSPARENT);

    if (a.hasValue(4))
        COLOR_TEXT_PRIMARY = a.getColor(4, Color.TRANSPARENT);

    if (a.hasValue(5))
        COLOR_TEXT_PRIMARY_INVERSE = a.getColor(5, Color.TRANSPARENT);

    if (a.hasValue(6))
        COLOR_PRIMARY = a.getColor(6, Color.TRANSPARENT);

    if (a.hasValue(7))
        COLOR_PRIMARY_DARK = a.getColor(7, Color.TRANSPARENT);

    if (a.hasValue(8))
        COLOR_TEXT_SECONDARY = a.getColor(8, Color.TRANSPARENT);

    if (a.hasValue(9))
        COLOR_BACKGROUND = a.getColor(9, Color.TRANSPARENT);

    if (a.hasValue(10))
        COLOR_TEXT_SECONDARY_INVERSE = a.getColor(10, Color.TRANSPARENT);

    a.recycle();//from   w ww.ja v a 2 s . c  o m

    CORNER_RADIUS = context.getResources().getDimensionPixelSize(R.dimen.control_corner_material);

    if (Config.DEBUG) {
        Log.i(TAG, "COLOR_ACCENT: " + Integer.toHexString(COLOR_ACCENT));
        Log.i(TAG, "COLOR_CONTROL_HIGHLIGHT: " + Integer.toHexString(COLOR_CONTROL_HIGHLIGHT));
        Log.i(TAG, "COLOR_CONTROL_ACTIVATED: " + Integer.toHexString(COLOR_CONTROL_ACTIVATED));
        Log.i(TAG, "COLOR_BUTTON_NORMAL: " + Integer.toHexString(COLOR_BUTTON_NORMAL));
        Log.i(TAG, "COLOR_TEXT_PRIMARY: " + Integer.toHexString(COLOR_TEXT_PRIMARY));
        Log.i(TAG, "COLOR_TEXT_PRIMARY_INVERSE: " + Integer.toHexString(COLOR_TEXT_PRIMARY_INVERSE));
        Log.i(TAG, "COLOR_PRIMARY: " + Integer.toHexString(COLOR_PRIMARY));
        Log.i(TAG, "COLOR_PRIMARY_DARK: " + Integer.toHexString(COLOR_PRIMARY_DARK));
        Log.i(TAG, "COLOR_TEXT_SECONDARY: " + Integer.toHexString(COLOR_TEXT_SECONDARY));
        Log.i(TAG, "COLOR_BACKGROUND: " + Integer.toHexString(COLOR_BACKGROUND));
        Log.i(TAG, "COLOR_TEXT_SECONDARY_INVERSE: " + Integer.toHexString(COLOR_TEXT_SECONDARY_INVERSE));
    }
}

From source file:com.appeaser.sublimepickerlibrary.utilities.SUtils.java

public static ContextThemeWrapper createThemeWrapper(Context context, int parentStyleAttr,
        int parentDefaultStyle, int childStyleAttr, int childDefaultStyle) {
    final TypedArray forParent = context.obtainStyledAttributes(new int[] { parentStyleAttr });
    int parentStyle = forParent.getResourceId(0, parentDefaultStyle);
    forParent.recycle();//from   ww  w .j  av a  2s.  c  om

    TypedArray forChild = context.obtainStyledAttributes(parentStyle, new int[] { childStyleAttr });
    int childStyleId = forChild.getResourceId(0, childDefaultStyle);
    forChild.recycle();

    return new ContextThemeWrapper(context, childStyleId);
}

From source file:com.android.deskclock.Utils.java

/**
 * Convenience method for retrieving a themed color value.
 *
 * @param context  the {@link Context} to resolve the theme attribute against
 * @param attr     the attribute corresponding to the color to resolve
 * @param defValue the default color value to use if the attribute cannot be resolved
 * @return the color value of the resolve attribute
 *///  w w w  . j  av  a2s  .co m
public static int obtainStyledColor(Context context, int attr, int defValue) {
    TEMP_ARRAY[0] = attr;
    final TypedArray a = context.obtainStyledAttributes(TEMP_ARRAY);
    try {
        return a.getColor(0, defValue);
    } finally {
        a.recycle();
    }
}

From source file:info.papdt.blacklight.support.Utility.java

public static int getLayerColor(Context context) {
    try {/*ww  w. j a va 2  s.  co m*/
        TypedArray array = context.obtainStyledAttributes(R.styleable.BlackLight);
        int ret = array.getColor(R.styleable.BlackLight_LayerColor, 0);
        array.recycle();
        return ret;
    } catch (NotFoundException e) {
        return 0;
    }
}

From source file:info.papdt.blacklight.support.Utility.java

public static int getFABBackground(Context context) {
    try {//from  ww  w.  j av a2 s.c  o m
        TypedArray array = context.obtainStyledAttributes(R.styleable.BlackLight);
        int ret = array.getColor(R.styleable.BlackLight_FABBackground, 0);
        array.recycle();
        return ret;
    } catch (NotFoundException e) {
        return 0;
    }
}