Convert Color to String in #FFFFFF format - Android Graphics

Android examples for Graphics:Color Operation

Description

Convert Color to String in #FFFFFF format

Demo Code


//package com.java2s;
import android.graphics.Color;

public class Main {

    public static String Color2String(int color) {
        String A = "";
        String R = "";
        String G = "";
        String B = "";
        try {//from   ww w.j  a  va 2 s.c o  m
            A = Integer.toHexString(Color.alpha(color));
            A = A.length() < 2 ? ('0' + A) : A;
            R = Integer.toHexString(Color.red(color));
            R = R.length() < 2 ? ('0' + R) : R;
            G = Integer.toHexString(Color.green(color));
            G = G.length() < 2 ? ('0' + G) : G;
            B = Integer.toHexString(Color.blue(color));
            B = B.length() < 2 ? ('0' + B) : B;
        } catch (Exception e) {
            return "#FFFFFF";
        }
        return '#' + A + R + G + B;
    }
}

Related Tutorials