Get background color of a Layout

To get background color of a Layout:

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

If It is RelativeLayout then just find its id and use there object instead of LinearLayout.


This can only be accomplished in API 11+ if your background is a solid color.

int color = Color.TRANSPARENT;
Drawable background = view.getBackground();
if (background instanceof ColorDrawable)
    color = ((ColorDrawable) background).getColor();