Get Background Color of a View in hex

Issue

I want to get background color of a view in hex format.

for example consider int getViewBackgroundColor(View view) my excepted return vaule is 0Xff256e78.

How could I do this?

thanks.

Solution

LinearLayout layout = (LinearLayout) findViewById(R.id.lay1);
ColorDrawable viewColor = (ColorDrawable) layout.getBackground();
int colorId = viewColor.getColor();

After getting as integer type of color, now you have to convert to hexadecimal:

String hexColor = String.format("#%06X", (0xFFFFFF & colorId));

Hope this helps..

Answered By – Nikhil Borad

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.