Why does the getResources (). Error getColor () is deprecated error when trying to change the background color?

    1 answer 1

    This is not an error, but a warning about the obsolescence of the method. Those. it will work, but, one day, it may stop. Now we need to use the ContextCompat#getColor(Context context, int color) method like this :

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

    If you look at the source of this method, you can see how it works:

     public static final int getColor(Context context, int id) { final int version = Build.VERSION.SDK_INT; if (version >= 23) { return ContextCompatApi23.getColor(context, id); } else { return context.getResources().getColor(id); } } 
    • Yeah, got it. And what is ContextCompat? - ikerya
    • And is it possible to write "in a new way" on Android 5.0.2? - ikerya
    • @ikerya, ContextCompat is a helper class from the support library for backward compatibility. As can be seen from the answer - allows you to get the color on all versions of the OS in one line instead of 4. You can use it on all versions of the OS. - JuriySPb
    • Well, what about the context ? - ikerya
    • one
      Thanks for the help! - ikerya