Example usage for android.content.res TypedArray getColor

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

Introduction

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

Prototype

@ColorInt
public int getColor(@StyleableRes int index, @ColorInt int defValue) 

Source Link

Document

Retrieve the color value for the attribute at index.

Usage

From source file:Main.java

public static int[] getColorsFromAttributes(Context context, int... attr) {
    final TypedArray a = context.obtainStyledAttributes(attr);
    int[] colors = new int[a.getIndexCount()];
    for (int i = 0; i < a.getIndexCount(); i++) {
        colors[i] = a.getColor(i, 0);
    }//from w  w w .j  a v  a2  s .co  m
    a.recycle();
    return colors;
}

From source file:Main.java

public static int[] getColorArray(@NonNull Context context, @ArrayRes int array) {
    if (array == 0)
        return null;
    TypedArray ta = context.getResources().obtainTypedArray(array);
    int[] colors = new int[ta.length()];
    for (int i = 0; i < ta.length(); i++)
        colors[i] = ta.getColor(i, 0);
    ta.recycle();// ww  w .  j  a v a  2s . co  m
    return colors;
}

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  ww w .  j a  va2  s . co  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

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.bobomee.android.recyclerviewhelperdemo.fragment.FastScrollFragment.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static int getColorAccent(Context context) {
    int accentAttr = android.R.attr.colorAccent;
    TypedArray androidAttr = context.getTheme().obtainStyledAttributes(new int[] { accentAttr });
    int colorAccent = androidAttr.getColor(0, 0xFF009688); //Default: material_deep_teal_500
    androidAttr.recycle();/*from  w  ww . ja v a2  s .com*/
    return colorAccent;
}

From source file:android.support.v7.content.res.AppCompatColorStateListInflater.java

/**
 * Fill in this object based on the contents of an XML "selector" element.
 *///from   w ww  . j av a 2s.  co m
private static ColorStateList inflate(@NonNull Resources r, @NonNull XmlPullParser parser,
        @NonNull AttributeSet attrs, @Nullable Resources.Theme theme)
        throws XmlPullParserException, IOException {
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    int type;
    int defaultColor = DEFAULT_COLOR;

    int[][] stateSpecList = new int[20][];
    int[] colorList = new int[stateSpecList.length];
    int listSize = 0;

    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG || depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }

        final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.ColorStateListItem);
        final int baseColor = a.getColor(R.styleable.ColorStateListItem_android_color, Color.MAGENTA);

        float alphaMod = 1.0f;
        if (a.hasValue(R.styleable.ColorStateListItem_android_alpha)) {
            alphaMod = a.getFloat(R.styleable.ColorStateListItem_android_alpha, alphaMod);
        } else if (a.hasValue(R.styleable.ColorStateListItem_alpha)) {
            alphaMod = a.getFloat(R.styleable.ColorStateListItem_alpha, alphaMod);
        }

        a.recycle();

        // Parse all unrecognized attributes as state specifiers.
        int j = 0;
        final int numAttrs = attrs.getAttributeCount();
        int[] stateSpec = new int[numAttrs];
        for (int i = 0; i < numAttrs; i++) {
            final int stateResId = attrs.getAttributeNameResource(i);
            if (stateResId != android.R.attr.color && stateResId != android.R.attr.alpha
                    && stateResId != R.attr.alpha) {
                // Unrecognized attribute, add to state set
                stateSpec[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId;
            }
        }
        stateSpec = StateSet.trimStateSet(stateSpec, j);

        // Apply alpha modulation. If we couldn't resolve the color or
        // alpha yet, the default values leave us enough information to
        // modulate again during applyTheme().
        final int color = modulateColorAlpha(baseColor, alphaMod);
        if (listSize == 0 || stateSpec.length == 0) {
            defaultColor = color;
        }

        colorList = GrowingArrayUtils.append(colorList, listSize, color);
        stateSpecList = GrowingArrayUtils.append(stateSpecList, listSize, stateSpec);
        listSize++;
    }

    int[] colors = new int[listSize];
    int[][] stateSpecs = new int[listSize][];
    System.arraycopy(colorList, 0, colors, 0, listSize);
    System.arraycopy(stateSpecList, 0, stateSpecs, 0, listSize);

    return new ColorStateList(stateSpecs, colors);
}

From source file:com.finchuk.clock2.timepickers.Utils.java

public static int getTextColorFromThemeAttr(Context context, int resid) {
    // http://stackoverflow.com/a/33839580/5055032
    //        final TypedValue value = new TypedValue();
    //        context.getTheme().resolveAttribute(resid, value, true);
    //        TypedArray a = context.obtainStyledAttributes(value.data,
    //                new int[] {resid});
    TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { resid });
    final int color = a.getColor(0/*index*/, 0/*defValue*/);
    a.recycle();/* w ww.j  a  v  a  2  s . c  o m*/
    return color;
    // Didn't work! Gave me white!
    //        return getColorFromThemeAttr(context, android.R.attr.textColorPrimary);
}

From source file:bander.notepad.Notepad.java

/**
 * Sets the color of the {@link android.support.v7.widget.Toolbar}
 *
 * @param activity Used similarly to {@link android.content.Context}
 *//* w ww  . j av a2 s. c o  m*/
public static void setToolbarColor(final ActionBarActivity activity) {
    SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(activity);

    /**
     * The lighter toolbar color {@link com.devin.notepad.R.array.color500}
     */
    TypedArray m500ta = activity.getResources().obtainTypedArray(R.array.color500);
    final int[] mToolbarColors = new int[m500ta.length()];
    for (int i = 0; i < m500ta.length(); i++) {
        mToolbarColors[i] = m500ta.getColor(i, 0);
    }
    m500ta.recycle();

    /**
     * The darker status bar color. {@link com.devin.notepad.R.array.color700}
     */
    TypedArray m700ta = activity.getResources().obtainTypedArray(R.array.color700);
    int[] statusBarColors = new int[m700ta.length()];
    for (int i = 0; i < m700ta.length(); i++) {
        statusBarColors[i] = m700ta.getColor(i, 0);
    }
    m700ta.recycle();

    final int abc = mSettings.getInt("actionBarColor", 0);

    Toolbar mToolbar = (Toolbar) activity.findViewById(R.id.toolbar);

    if (mToolbar != null)
        mToolbar.setBackgroundColor(mToolbarColors[abc]);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        activity.getWindow().setStatusBarColor(statusBarColors[abc]);
    }
}

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
 *///from   w  ww  .ja v  a  2s .  c o 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:com.desno365.mods.DesnoUtils.java

public static Snackbar getDefaultSnackbar(View parent, int text, int duration) {
    Snackbar snack = Snackbar.make(parent, text, duration);

    // make text white
    View view = snack.getView();//from w  w  w.  j av  a 2 s. c o  m
    @SuppressLint("PrivateResource")
    TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
    tv.setTextColor(Color.WHITE);

    // set background and action colors
    TypedArray a = parent.getContext().getTheme()
            .obtainStyledAttributes(new int[] { R.attr.color_primary_dark, R.attr.color_accent });
    int color1 = a.getColor(0, 0);
    int color2 = a.getColor(1, 0);
    a.recycle();
    view.setBackgroundColor(color1);
    snack.setActionTextColor(color2);

    return snack;
}