Set Background Color of Android View Programmatically

While you are generating or displaying content through list views, it is sometimes you need to make a zebra-styled rows or coloring the view of row background into a custom color depending on row’s content.

This snippets is applicable to any android view which is inheriting View class. So to change or modify view’s background color, it is as easy as the following code on your activity class:

View v = this.findViewById(R.id.yourviewid);
v.setBackgroundColor(0xFFFF0000);

In the above example, background color of view with matching id yourviewid will be painted RED. Red color is indicated by 6 hex digits #FF0000 (RGB) while the preceeding FF hex right after the 0x character is the alpha (transparency) value of given color. Alpha value of 00 means full transparency, the color is invisible, while FF means no transparency, the color is fully visible.