How to get accent color programmatically?

前端 未结 7 2363
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 03:32

How would one fetch the accent color set in styles, like below, programmatically?

    @color/material_green_500<         


        
相关标签:
7条回答
  • 2020-12-05 04:16

    You can fetch it from the current theme in this way:

    private int fetchAccentColor() {
        TypedValue typedValue = new TypedValue();
    
        TypedArray a = mContext.obtainStyledAttributes(typedValue.data, new int[] { R.attr.colorAccent });
        int color = a.getColor(0, 0);
    
        a.recycle();
    
        return color;
    }
    
    0 讨论(0)
  • 2020-12-05 04:16

    This worked for me as well:

    public static int getThemeAccentColor (final Context context) {
        final TypedValue value = new TypedValue ();
        context.getTheme ().resolveAttribute (R.attr.colorAccent, value, true);
        return value.data;
    }
    
    0 讨论(0)
  • 2020-12-05 04:20

    Here's my take on this:

    public static String getThemeColorInHex(@NonNull Context context, @NonNull String colorName, @AttrRes int attribute) {
        TypedValue outValue = new TypedValue();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            context.getTheme().resolveAttribute(attribute, outValue, true);
        } else {
            // get color defined for AppCompat
            int appCompatAttribute = context.getResources().getIdentifier(colorName, "attr", context.getPackageName());
            context.getTheme().resolveAttribute(appCompatAttribute, outValue, true);
        }
        return String.format("#%06X", (0xFFFFFF & outValue.data));
    }
    

    Usage:

        String windowBackgroundHex = getThemeColorInHex(this, "windowBackground", android.R.attr.windowBackground);
        String primaryColorHex = getThemeColorInHex(this, "colorPrimary", R.attr.colorPrimary);
    
    0 讨论(0)
  • 2020-12-05 04:21

    I have an static method on a utils class to get the colors from the current theme. Most of times is colorPrimary, colorPrimaryDark and accentColor, but you can get a lot more.

    @ColorInt
    public static int getThemeColor
    (
            @NonNull final Context context,
            @AttrRes final int attributeColor
    )
    {
        final TypedValue value = new TypedValue();
        context.getTheme ().resolveAttribute (attributeColor, value, true);
        return value.data;
    }
    
    0 讨论(0)
  • 2020-12-05 04:28
    private static int getThemeAccentColor(Context context) {
        int colorAttr;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            colorAttr = android.R.attr.colorAccent;
        } else {
            //Get colorAccent defined for AppCompat
            colorAttr = context.getResources().getIdentifier("colorAccent", "attr", context.getPackageName());
        }
        TypedValue outValue = new TypedValue();
        context.getTheme().resolveAttribute(colorAttr, outValue, true);
        return outValue.data;
    }
    
    0 讨论(0)
  • 2020-12-05 04:30

    For those of you using Kotlin

    fun Context.themeColor(@AttrRes attrRes: Int): Int {
        val typedValue = TypedValue()
        theme.resolveAttribute (attrRes, typedValue, true)
        return typedValue.data
    }
    
    0 讨论(0)
提交回复
热议问题