Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set color using integer?

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.

like image 351
Arun Avatar asked Dec 13 '11 13:12

Arun


People also ask

How do you convert RGB to integer?

So far I use the following to get the RGB values from it: // rgbs is an array of integers, every single integer represents the // RGB values combined in some way int r = (int) ((Math. pow(256,3) + rgbs[k]) / 65536); int g = (int) (((Math. pow(256,3) + rgbs[k]) / 256 ) % 256 ); int b = (int) ((Math.

How can I change the color of INT in Android?

The four components of a color int are encoded in the following way: int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);

What color formats are supported for color resources?

You can also use a color resource when a drawable resource is expected in XML (for example, android:drawable="@color/green" ). The value always begins with a pound (#) character and then followed by the Alpha-Red-Green-Blue information in one of the following formats: #RGB. #ARGB.


1 Answers

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

like image 169
Arun Avatar answered Oct 28 '22 04:10

Arun