Java – Android – Convert ARGB colors to HEX

Android – Convert ARGB colors to HEX… here is a solution to the problem.

Android – Convert ARGB colors to HEX

I have an ARGB color (looks like 255 200 200 000). I tried to convert it to hexadecimal format with this code :


    String col = "#" + Integer.toString(Color.alpha(img.getPixel(j, i)), 16) + 
        Integer.toString(Color.red(img.getPixel(j, i)), 16) + 
        Integer.toString(Color.green(img.getPixel(j, i)), 16) + 
        Integer.toString(Color.blue(img.getPixel(j, i)), 16);

But I get (#FFC8C8) instead of (#FFC8C800). So all numbers less than 10 are without zeros.
How do I fix that code to make it work?

P.S. Please forgive my English

Solution

You can use

it

String hexColor = String.format("#%08X", img.getPixel(j, i));

Related Problems and Solutions