Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Color.RED code into integer values of color in Android Java? [duplicate]

In my android projects i set color for PieChart like this

public static int[] COLORS = new int[] { Color.GREEN, Color.BLUE };

In that import android.graphics.Color; have only few colors around 10. I need more color so i tried to set as integer but didn't working. Color.alpha(16777184) for light yello color. this code is not working ? how to set here as integer value of color...?

like image 932
Sri Avatar asked Aug 19 '14 11:08

Sri


People also ask

How do you convert RGB to integer?

To set the values from RGB you can do so by: Color myColour = new Color(red, green, blue); int rgb = myColour. getRGB(); //Change the pixel at (x,y) ti rgb value image. setRGB(x, y, rgb);

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);


2 Answers

I'd more preffer to use XML file. Suggested solution by Kruba Patel will in fact force developer to remember HEX of color and also redundante of code.

Here XML resource example just call R.color.color_name please note that R.color.color_name will return generated int value :)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color
        name="color_name"
        >hex_color</color>
</resources>
like image 22
Robert Avatar answered Oct 27 '22 16:10

Robert


Try this:

Color.parseColor("#FF0000")

int color = 0xFFFF0000;

Hope this may help you!

like image 147
Krupa Patel Avatar answered Oct 27 '22 17:10

Krupa Patel