Reputation: 356
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?
Upvotes: 2
Views: 3686
Reputation: 148
use: snackbarView.setBackgroundColor(Color.RED);
not: snackbarView.setBackgroundColor(ContextCompat.getColor(context, Color.RED));
Reason:
Look at the official android developer site.
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,
it needs resource id, not color id. Hope, now, you can understand this matter.
Upvotes: 6
Reputation: 75778
You are getting
Resources$NotFoundException: Resource ID #0xffff0000
This exception is thrown by the resource APIs when a requested resource can not be found.
Create custom colors.xml
which holds colors .
res/values/colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff0000 </color>
</resources>
Then
setBackgroundColor(ContextCompat.getColor(context, R.color.red));
Or Simple
setBackgroundColor(Color.parseColor("#ff0000"));
Upvotes: 2
Reputation: 630
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));
Upvotes: 5
Reputation: 147
You treat Color.RED(an int value) as a resourceId. Logical mistake!
just use Color.RED insted.
snackbarView.setBackgroundColor(Color.RED)
Upvotes: 0
Reputation: 895
Color.RED is a color not an ID. You should do this:
snackbarView.setBackgroundColor(Color.RED);
Upvotes: 0
Reputation: 4220
Try setting background color like this:
snackbarView.setBackgroundColor(ContextCompat.getColor(context, R.color.RED));
Upvotes: 0