Search code examples
androiduser-interfaceviewcolorsbackground

Get Background Color of a View in hex


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..