How to set color using integer?

Arun picture Arun · Dec 13, 2011 · Viewed 21.2k times · Source

How can i convert color code in integer ex: 13369395 to android specific. Since 13369395 is also an integer i tried doing

mainLayout.setTextColor(13369395);

but its not working.

I also tried converting 13369395 to hexadecimal like:

mainLayout.setBackgroundColor(Integer.parseInt(13369395 +"", 16)+0xFF000000);

but it also didn't help.

Answer

Arun picture Arun · Dec 14, 2011

I got the solution. Just a work around with Hexadecimal as below:

Integer.toHexString(colour);

Which returns the hexadecimal string for your integer, again if you just use it by

mainLayout.setBackgroundColor(Integer.parseInt(hexVal,16));

it wont work. You need to add mask as

mainLayout.setBackgroundColor(0xff000000 + Integer.parseInt(hexVal,16));

This has resolved the problem