Tuhin Subhra
Tuhin Subhra

Reputation: 356

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?

Upvotes: 2

Views: 3686

Answers (6)

Farhan Rahman Arnob
Farhan Rahman Arnob

Reputation: 148

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.

Upvotes: 6

IntelliJ Amiya
IntelliJ Amiya

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

Rajshree Tiwari
Rajshree Tiwari

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

jiashie
jiashie

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

Furqan
Furqan

Reputation: 895

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

snackbarView.setBackgroundColor(Color.RED);

Upvotes: 0

Ratilal Chopda
Ratilal Chopda

Reputation: 4220

Try setting background color like this:

snackbarView.setBackgroundColor(ContextCompat.getColor(context, R.color.RED));

Upvotes: 0

Related Questions