Home » Android » android – getColor Error in API Level 23

android – getColor Error in API Level 23

Posted by: admin May 14, 2020 Leave a comment


getResources().getColor(R.color.color_name) is now deprecated in API Level 23 but can work by adding a Color Theme as a second parameter like

getResources().getColor(R.color.color_name, Theme)
according to the new documentation but when I pass in null value for the Theme like

getResources().getColor(R.color.color_name, null)
my app crashes. Maybe I am missing something in my understanding. Please help, thanx in advance.

How to&Answers:

The old method is deprecated starting in API 23, and the new method only exists in API 23+. You are attempting to call the new method on a device running API <23.

You can either perform an API level check and call the appropriate method, or you can use ContextCompat.getColor(Context, int) from the support-v4 library.


Try this..

int color = Color.parseColor(getResources().getString(R.color.color_name));


int color = getResources().getColor(R.color.color_name);


As mentioned here, you can use ContextCompat as follows:

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