Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 739
Nick R Avatar asked Nov 13 '22 11:11

Nick R


1 Answers

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

like image 72
user1234567 Avatar answered Nov 16 '22 02:11

user1234567