Resolved color instead of a resource id

AndroidLint

Android Problem Overview


Recently I've seen appeared a lint error in my code:

> Should pass resolved color instead of resource id here: > getResources().getColor(R.color.maps_list_background_color) > MyClass.java /myapp/android/maps line 107 Android Lint Problem

I know how to resolve it the answer is in the error, the thing is I don't get why they have added this error in the linter.

Android Solutions


Solution 1 - Android

>Methods that take a color in the form of an integer should be passed an RGB triple, not the actual color resource id. You must call getResources.getColor(resource).

The function you are calling is expecting an integer that is an RGB triple, not just the id of a color resource. The color resource id is still an integer, but would not produce the color that you are expecting if it was used as the RGB triple. In order to pass it the correct RGB triple for your color, you must resolve it with the getResources().getColor(R.color.example_color) call.

Solution 2 - Android

Since I'm still finding this on Google and it is deprecated, I thought I'd might as well share the current method of doing this.

check https://stackoverflow.com/questions/31842983/getresources-getcolor-is-deprecated

ContextCompat.getColor(getApplicationContext(), R.color.color_name)

Solution 3 - Android

Use annotation @ColorInt to confirm that this is color not a color reference id.

See: android.support.annotation.ColorInt

Solution 4 - Android

Since getResources().getColor() is deprecated, you need to do this to get the color:

int color = ContextCompat.getColor(getContext(),your_color_id);

Now you have the color with respect to the current context Set the color using:

your_view.setBackgroundColor(color);

Solution 5 - Android

As for me, it's very stupid warning.

I have own class with function:

public static final void setBackgroundColor(View v, int id) {
// Here I get color by id from resources and setBackgroundColor of this color.
}

Anyway, if I try call setBackgroundColor, I get warning. But why?

So, I did simple: rename setBackgroundColor to setBackgroundColorr.

This warning activate if found name color at function name.

And yes, I do not like name setColorBackground or any other :-)

Solution 6 - Android

If you're using androidx, you can do:

requireContext().getColor(R.color.myColor);

Solution 7 - Android

Apparently this is caused by lint; third bullet down.

New Lint Rules

You could probably surpress this, or try implementing their syntax.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeozView Question on Stackoverflow
Solution 1 - Androidter0View Answer on Stackoverflow
Solution 2 - AndroidRaymondView Answer on Stackoverflow
Solution 3 - AndroidMaher AbuthraaView Answer on Stackoverflow
Solution 4 - AndroidtsukiView Answer on Stackoverflow
Solution 5 - AndroidYura ShinkarevView Answer on Stackoverflow
Solution 6 - AndroidAndrWeisRView Answer on Stackoverflow
Solution 7 - AndroidJonView Answer on Stackoverflow