130

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

Tima
  • 12,765
  • 23
  • 82
  • 125

10 Answers10

175

This is your answer

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

you will get

 colorStr = "#123456"
Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
  • 7
    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 :( – Tima Feb 17 '11 at 09:47
  • of course, I can cut 'ff' out, but I try to solve my problem in other way – Tima Feb 17 '11 at 09:47
  • I wanted to use html tags, but i think I'll just use setTextColor(id) – Tima Feb 17 '11 at 09:59
  • No issue.Then what was the purpose of raise the question? – Tanmay Mandal Feb 17 '11 at 10:08
  • sorry, I wanted to make my life easier. And common. I've learn something new :) – Tima Feb 17 '11 at 10:12
  • and now i know, why did i want to use html-tags instead of setTextColor. The setTextColor didn't work. But i found solution for this as well http://stackoverflow.com/questions/1466788/android-textview-setting-the-background-color-dynamically-doesnt-work/2112933#2112933 – Tima Feb 17 '11 at 10:29
  • 7
    A simple substring of the returned color string to remove the Alpha works. getResources().getString(R.color.my_colour).substring(3); – scottyab Nov 27 '12 at 15:35
  • 95
    This is not working any more, error 'Expected resource of type String' – Clive Jefferies Mar 05 '15 at 15:29
  • 25
    @CliveJefferies You can add `//noinspection ResourceType` just above your statement. – lordmegamax Mar 20 '15 at 12:40
  • 23
    `getResources().getString(0+R.color.someColor);` works – 18446744073709551615 Feb 02 '16 at 01:17
  • @lordmegamax what do you mean by that? Adding that line is just a comment and has no effect. – CACuzcatlan Feb 22 '16 at 01:44
  • 1
    @CACuzcatlan It's not just a comment, it's an annotation for lint. http://tools.android.com/tips/lint/suppressing-lint-warnings – lordmegamax Feb 22 '16 at 13:52
  • @scottyab `substring(3)` doesn't solve the problem since it also remove the `#` and you end up with `123456` instead of `#123456`. – Tot Zam Jan 17 '17 at 02:44
  • @CliveJefferies is that you ?? – Utsav Gupta Sep 05 '17 at 13:26
  • Not useful having trouble of param type mismatch. – Hassan Jamil Sep 26 '17 at 07:31
  • 4
    This will give error as, "Expected Resources of type String" Use the below code for getting the hex value. ```String colorStr = "#" + Integer.toHexString (getResources().getColor(R.color.colorPrimaryDark));``` – Sagar Giri Mar 08 '18 at 07:12
  • Expected resource of type string less... Ensures that resource id's passed to APIs are of the right type; for example, calling Resources.getColor(R.string.name) is wrong – Alireza Noorali Apr 30 '18 at 12:43
  • 1
    Just add `@SuppressLint("ResourceType")` above the `getString` call like this: `@SuppressLint("ResourceType") String someColorHex = resources.getString(R.color.a_color);` – m_katsifarakis Sep 28 '18 at 12:50
  • @Tanmay Mandal Getting Expected resource of type string after using colorStr=getResources().getString(R.color.someColor); – Prateek Nov 08 '19 at 12:13
161

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);
kodi
  • 2,195
  • 1
  • 14
  • 20
  • 8
    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` – gattsbr Jul 09 '15 at 19:22
  • 4
    geColor() need api > 23 – Honghe.Wu Aug 19 '16 at 09:28
  • 1
    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. – Wes Winn Apr 25 '17 at 23:00
  • Integer.toHexString(ContextCompat.getColor(context, R.color.black) & 0x00ffffff); – AITAALI_ABDERRAHMANE May 09 '17 at 17:24
  • 10
    This won't work if there is 0 at first of the hex color. – asish Jun 12 '17 at 12:17
48

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)
Florian M
  • 2,815
  • 3
  • 17
  • 14
27

The answers provided above are not updated.

Please try this one

String colorHex = "#" + Integer.toHexString(ContextCompat.getColor(getActivity(), R.color.dark_sky_blue) & 0x00ffffff);
Darush
  • 11,403
  • 9
  • 62
  • 60
solidak
  • 5,033
  • 3
  • 31
  • 33
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 );

EMalik
  • 2,446
  • 1
  • 26
  • 13
Honghe.Wu
  • 5,899
  • 6
  • 36
  • 47
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

Laurel
  • 5,965
  • 14
  • 31
  • 57
Samarth S
  • 313
  • 4
  • 5
6

It works for me!

String.format("#%06x", ContextCompat.getColor(this, R.color.my_color) & 0xffffff)
Carlos Galindo
  • 101
  • 1
  • 6
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>";
    }
Tuan Nguyen
  • 2,542
  • 19
  • 29
1

This is how I've done it:

String color = "#" + Integer.toHexString(ContextCompat.getColor
(getApplicationContext(), R.color.yourColor) & 0x00ffffff);
dginelli
  • 409
  • 6
  • 14
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;

Nick
  • 1,733
  • 1
  • 14
  • 24