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.
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));
instead
int color = getResources().getColor(R.color.color_name);
As mentioned here, you can use ContextCompat as follows:
ContextCompat.getColor(context, R.color.color_name);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With