getColor(int id) deprecated on Android 6.0 Marshmallow (API 23)

前端 未结 11 1834
我寻月下人不归
我寻月下人不归 2020-11-22 12:29

The Resources.getColor(int id) method has been deprecated.

@ColorInt
@Deprecated
public int getColor(@ColorRes int id) throws NotFoundException          


        
相关标签:
11条回答
  • 2020-11-22 13:05

    I got frustrated too. My need was very straightforward. All I wanted was the ARGB color from the resources, so I wrote a simple static method.

    protected static int getARGBColor(Context c, int resId)
            throws Resources.NotFoundException {
    
        TypedValue color = new TypedValue();
        try {
            c.getResources().getValue(resId, color, true);
        }
        catch (Resources.NotFoundException e) {
            throw(new Resources.NotFoundException(
                      String.format("Failed to find color for resourse id 0x%08x",
                                    resId)));
        }
        if (color.type != TYPE_INT_COLOR_ARGB8) {
            throw(new Resources.NotFoundException(
                      String.format(
                          "Resourse id 0x%08x is of type 0x%02d. Expected TYPE_INT_COLOR_ARGB8",
                          resId, color.type))
            );
        }
        return color.data;
    }
    
    0 讨论(0)
  • 2020-11-22 13:10

    tl;dr:

    ContextCompat.getColor(context, R.color.my_color)
    

    Explanation:

    You will need to use ContextCompat.getColor(), which is part of the Support V4 Library (it will work for all the previous APIs).

    ContextCompat.getColor(context, R.color.my_color)
    

    If you don't already use the Support Library, you will need to add the following line to the dependencies array inside your app build.gradle (note: it's optional if you already use the appcompat (V7) library):

    compile 'com.android.support:support-v4:23.0.0' # or any version above
    

    If you care about themes, the documentation specifies that:

    Starting in M, the returned color will be styled for the specified Context's theme

    0 讨论(0)
  • 2020-11-22 13:10

    In Android Marshmallow many methods are deprecated.

    For example, to get color use

    ContextCompat.getColor(context, R.color.color_name);
    

    Also to get drawable use

    ContextCompat.getDrawable(context, R.drawable.drawble_name);
    
    0 讨论(0)
  • 2020-11-22 13:14

    If your current min. API level is 23, you can simply use getColor() like we are using to get string resources by getString():

    //example
    textView.setTextColor(getColor(R.color.green));
    // if `Context` is not available, use with context.getColor()
    

    You can constraint for API Levels below 23:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        textView.setTextColor(getColor(R.color.green));
    } else {
        textView.setTextColor(getResources().getColor(R.color.green));
    }
    

    but to keep it simple, you can do like below as accepted answer:

    textView.setTextColor(ContextCompat.getColor(context, R.color.green))
    

    From Resources.

    From ContextCompat AndroidX.

    From ContextCompat Support

    0 讨论(0)
  • 2020-11-22 13:16

    Use the getColor(Resources, int, Theme) method of the ResourcesCompat from the Android Support Library.

    int white = new ResourcesCompat().getColor(getResources(), R.color.white, null);
    

    I think it reflect better your question than the getColor(Context, int) of the ContextCompat since you ask about Resources. Prior to API level 23, the theme will not be applied and the method calls through to getColor(int) but you'll not have the deprecated warning. The theme also may be null.

    0 讨论(0)
  • 2020-11-22 13:19

    Starting from Android Support Library 23,
    a new getColor() method has been added to ContextCompat.

    Its description from the official JavaDoc:

    Returns a color associated with a particular resource ID

    Starting in M, the returned color will be styled for the specified Context's theme.


    So, just call:

    ContextCompat.getColor(context, R.color.your_color);
    

    You can check the ContextCompat.getColor() source code on GitHub.

    0 讨论(0)
提交回复
热议问题