Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is getResources().getColor(int) deprecated?

The document says:

This method was deprecated in API level 23. Use getColor(int, Theme) instead.

And many posts point to the ContextCompat.getColor(Context, int) method as a replacement.

Also the document explains the Theme parameter:

theme Resources.Theme: The theme used to style the color attributes, may be null.

Can you explain how can the theme affects the color?

like image 523
ssynhtn Avatar asked Dec 08 '25 10:12

ssynhtn


1 Answers

Some complex colors like android.content.res.GradientColor (which are used inside a VectorDrawable) need a Theme in order to inflate the gradient, since you could have a definition like:

<gradient xmlns:android="http://schemas.android.com/apk/res/android">
   <android:startColor="?android:attr/colorPrimary"/>
   <android:endColor="?android:attr/colorControlActivated"/>
   <android:type="linear"/>
 </gradient>
like image 170
Logain Avatar answered Dec 10 '25 22:12

Logain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!