Java – Can’t retrieve colors from colors.xml via custom resource?

Can’t retrieve colors from colors.xml via custom resource?… here is a solution to the problem.

Can’t retrieve colors from colors.xml via custom resource?

I’m working on an Android tutorial and I’m trying to access the colors I defined in colors.xml

<color name = "my_background">#3500ffff</color>

Then I tried to access this color by name :

Paint background = new Paint();
background.setColor(getResources().getColor(R.color.my_background));

However, it does not recognize the my_background as a resource. If I let the suggestion appear, just a bunch of irrelevant predefined colors pop up. Any ideas? This doesn’t make sense to me, I started with developer documentation See almost exactly the same thing as Another site, but that didn’t work for me.

Solution

I found the problem with import android. R somehow got to the top of my code without me noticing. I deleted it and the problem was solved.

Related Problems and Solutions