135

If i defined a color in resources

<resources>
    <color name="someColor">#123456</color>
</resources>

it's possible to set color by its id, like

view.setTextColor(R.color.someColor);

Is it also possible to get color string value from colors.xml?

Something like

colorStr = getColor(R.color.someColor);
// -> colorStr = "#123456"

If yes, can anybody give an example?

Thank you

10 Answers 10

176

This is your answer

colorStr=getResources().getString(R.color.someColor);

you will get

 colorStr = "#123456"
Sign up to request clarification or add additional context in comments.

20 Comments

first, thank you. I did't thought to use getString for colors. It works, but instead of colorStr = "#123456" i'm getting "#ff123456", and this is not nice :(
A simple substring of the returned color string to remove the Alpha works. getResources().getString(R.color.my_colour).substring(3);
This is not working any more, error 'Expected resource of type String'
@CliveJefferies You can add //noinspection ResourceType just above your statement.
getResources().getString(0+R.color.someColor); works
|
164

Just for the sake of easy copypasta:

"#" + Integer.toHexString(ContextCompat.getColor(getActivity(), R.color.some_color));

Or if you want it without the transparency:

"#" + Integer.toHexString(ContextCompat.getColor(getActivity(), R.color.some_color) & 0x00ffffff);

5 Comments

This worked great, however I needed to modify this by using '#' + Integer.toHexString(getResources().getColor(R.color.someColor); since later I was using it in Color.parseColor
geColor() need api > 23
You can use the deprecated getColor() no problem. Just delete the "getColor(...)" and type it out again, use intellisense to complete the method call with the Android deprecated getColor call and you're good.
Integer.toHexString(ContextCompat.getColor(context, R.color.black) & 0x00ffffff);
This won't work if there is 0 at first of the hex color.
51

All of the solutions here using Integer.toHexString() break if you would have leading zeroes in your hex string. Colors like #0affff would result in #affff. Use this instead:

String.format("#%06x", ContextCompat.getColor(this, R.color.your_color) & 0xffffff)

or with alpha:

String.format("#%08x", ContextCompat.getColor(this, R.color.your_color) & 0xffffffff)

1 Comment

in Kotlin: String.format("#%06x", ContextCompat.getColor(context, color) and 0xffffff)
28

The answers provided above are not updated.

Please try this one

String colorHex = "#" + Integer.toHexString(ContextCompat.getColor(getActivity(), R.color.dark_sky_blue) & 0x00ffffff);

1 Comment

This worked well for me
10

Cause getResources().getColor need api > 23. So this is better: Just for the sake of easy copy & paste:

Integer.toHexString( ContextCompat.getColor( getContext(), R.color.someColor ) );

Or if you want it without the transparency:`

Integer.toHexString( ContextCompat.getColor( getContext(), R.color.someColor ) & 0x00ffffff );

2 Comments

ContextCompat.getColor does not take 3 arguments
@Nashe it is 2 arguments, not 3!
7

It works for me!

String.format("#%06x", ContextCompat.getColor(this, R.color.my_color) & 0xffffff)

Comments

6

For API above 21 you can use

getString(R.color.color_name);

This will return the color in a string format. To convert that to a color in integer format (sometimes only integers are accepted) then:

Color.parseColor(getString(R.color.color_name));

The above expression returns the integer equivalent of the color defined in color.xml file

Comments

2

Add @SuppressLint("ResourceType") if an error occurs. Like bellow.

private String formatUsernameAction(UserInfo userInfo, String action) {
        String username = userInfo.getUsername();
        @SuppressLint("ResourceType") String usernameColor = getContext().getResources().getString(R.color.background_button);
        return "<font color=\""+usernameColor+"\">" + username
                + "</font> <font color=\"#787f83\">" + action.toLowerCase() + "</font>";
    }

Comments

1

I don't think there is standard functionality for that. You can however turn the return in value from getColor() to hex and turn the hex value to string.

hex 123456 = int 1193046;

Comments

1

This is how I've done it:

String color = "#" + Integer.toHexString(ContextCompat.getColor
(getApplicationContext(), R.color.yourColor) & 0x00ffffff);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.