Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources$NotFoundException while trying to change color

I am trying to change color of my snackbar

snackbarView.setBackgroundColor(ContextCompat.getColor(context, Color.RED));

I am getting something like this :

android.content.res.Resources$NotFoundException: Resource ID #0xffff0000

Where definitely

0xffff0000

represents RED.But why it cant find this resource? Any help?

like image 810
Tuhin Subhra Avatar asked Jan 29 '26 02:01

Tuhin Subhra


2 Answers

use: snackbarView.setBackgroundColor(Color.RED);

not: snackbarView.setBackgroundColor(ContextCompat.getColor(context, Color.RED));

Reason:

Look at the official android developer site. it says that it need a color id.

So, you can directly add a color to it. No need to add ContextCompat.getColor() method to it.

If you want to use this, please use a valid resource id, not color id as the second parameter of the getColor method. Because from official website it says,

enter image description here

it needs resource id, not color id. Hope, now, you can understand this matter.

like image 146
Farhan Rahman Arnob Avatar answered Jan 31 '26 17:01

Farhan Rahman Arnob


Try this :-

In your values.xml , create colors.xml and add following line :

<color name="red">#FF0000</color>

Then call this color like this :-

snackbarView.setBackgroundColor(ContextCompat.getColor(context,R.color.red));
like image 25
Rajshree Tiwari Avatar answered Jan 31 '26 19:01

Rajshree Tiwari



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!