Android get color as string value

AndroidColors

Android Problem Overview


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

Android Solutions


Solution 1 - Android

This is your answer

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

you will get

 colorStr = "#123456"

Solution 2 - Android

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);

Solution 3 - Android

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)

Solution 4 - Android

The answers provided above are not updated.

Please try this one

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

Solution 5 - Android

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 );

Solution 6 - Android

It works for me!

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

Solution 7 - Android

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

Solution 8 - Android

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>";
    }

Solution 9 - Android

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;

Solution 10 - Android

This is how I've done it:

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

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
QuestionTimaView Question on Stackoverflow
Solution 1 - AndroidTanmay MandalView Answer on Stackoverflow
Solution 2 - AndroidkodiView Answer on Stackoverflow
Solution 3 - AndroidFlorian MView Answer on Stackoverflow
Solution 4 - AndroidsolidakView Answer on Stackoverflow
Solution 5 - AndroidHonghe.WuView Answer on Stackoverflow
Solution 6 - AndroidCarlos GalindoView Answer on Stackoverflow
Solution 7 - AndroidSamarth SView Answer on Stackoverflow
Solution 8 - AndroidTuan NguyenView Answer on Stackoverflow
Solution 9 - AndroidNickView Answer on Stackoverflow
Solution 10 - AndroiddginelliView Answer on Stackoverflow