Android get color as string value

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

The answers provided above are not updated.

Please try this one

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

This is your answer

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

you will get

 colorStr = "#123456"

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)

Tags:

Colors

Android