Code example for TypedArray

Methods: getColorStateList, getIndex, recycle

0
    public static final int SELECTOR_BACKGROUND_COLOR = android.R.attr.listSelector;
    public static final int DETAILS_ELEMENT_BACKGROUND = android.R.attr.detailsElementBackground;
 
    public static ColorStateList getSystemAttrColor(Context context, int attr) {
        TypedArray a = context.obtainStyledAttributes(new int[]{attr});
        ColorStateList color = a.getColorStateList(a.getIndex(0));
        a.recycle();
        return color;
    } 
 
    public static ColorStateList getTextColorPrimary(Context context) {
        return getSystemAttrColor(context, TEXT_COLOR_PRIMARY);
    } 
 
    public static ColorStateList getTextColorSecondary(Context context) {
        return getSystemAttrColor(context, TEXT_COLOR_SECONDARY);
    } 
 
    public static ColorStateList getSelectorBackgroundColor(Context context) {
        return getSystemAttrColor(context, SELECTOR_BACKGROUND_COLOR);
    }