2

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?

Tuhin Subhra
  • 356
  • 5
  • 17
  • 5
    `Color.RED` is an actual color value, not a resource name. If you want `Color.RED`, just use it directly - `setBackgroundColor(Color.RED)`. – Mike M. Apr 06 '17 at 07:19
  • Done,Thanks @MikeM. Actually I was confused because they raised an issue here: http://stackoverflow.com/questions/34020891/how-to-change-background-color-of-the-snackbar – Tuhin Subhra Apr 06 '17 at 07:26
  • 1
    Anything that starts with `R.color` is a resource name for a color resource in your project, and for those you would use `ContextCompat.getColor()` to get the actual value. The `Color` class, however, has several constants - e.g., `Color.RED`, `Color.BLUE`, etc. - that are actual color values. You don't need to pull those from your resources. – Mike M. Apr 06 '17 at 07:30
  • @TuhinSubhra try my answer , it is working properly ..:) – Rajshree Tiwari Apr 06 '17 at 07:33
  • @RajshreeTiwari,True that,Its working.Thanks. – Tuhin Subhra Apr 06 '17 at 07:38
  • @TuhinSubhra your welcome :) – Rajshree Tiwari Apr 06 '17 at 07:39

6 Answers6

6

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.

  • 2
    While this code may answer the question, providing additional [context](https://meta.stackexchange.com/q/114762) regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit](http://stackoverflow.com/posts/43248434/edit) your answer to add an explanation, and give an indication of what limitations and assumptions apply. It also doesn't hurt to mention why this answer is more appropriate than others. – Dev-iL Apr 06 '17 at 18:58
  • Thanks. I will provide it. – Farhan Rahman Arnob Apr 07 '17 at 20:02
5

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));
Rajshree Tiwari
  • 630
  • 1
  • 7
  • 27
2

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"));
Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

Try setting background color like this:

snackbarView.setBackgroundColor(ContextCompat.getColor(context, R.color.RED));
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
0

Color.RED is a color not an ID. You should do this:

snackbarView.setBackgroundColor(Color.RED);

Furqan
  • 787
  • 2
  • 13
  • 28
0

You treat Color.RED(an int value) as a resourceId. Logical mistake! just use Color.RED insted. snackbarView.setBackgroundColor(Color.RED)

jiashie
  • 147
  • 1
  • 11