Nick R
Nick R

Reputation: 51

Android Using a Color Resource value in Code

I have set a color in my resource colors.xml file. This works fine for TextViews etc

<color name="medsListItem">#980000</color>

I am building some html/ strings in code and wanted to use same colors as in my app and keep everything well organised

I am using the code below to get the color from the resource above

String colorToUse = (String) getResources().getString(R.color.medsListItem);

the string produced however is #ff980000 Android is adding ff into my string at characters 2 and 3 (or replacing # with #ff at front of string). I can get around this by adding another line in code

colorToUse = "#" + colorToUse.substring(3, 9);

but I think I am missing something as it is (a) inelegant and (b) I do not know why the ff is being added (guessing it is to do with how android handles the color value)

Upvotes: 5

Views: 2766

Answers (1)

user1234567
user1234567

Reputation: 1842

The returned color is in #AARRGGBB format, AA is the alpha value. This is described at the very beginning of this document: document link

Upvotes: 1

Related Questions