Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Android background color not visible?

I'm trying to figure out one simple thing: how to set a background color in Android view. Here is the code in an Activity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    View v = new  View(this);

    setContentView(v);
    v.setBackgroundColor(23423425);
}

and all I get is black screen.

like image 996
givi Avatar asked Nov 27 '22 12:11

givi


2 Answers

The integer you set is easier represented as a hex value. The hex values are 0xAARRGGBB.

  • A - represents the Alpha value which is how transparent the color is. A value of FF means it's not transparent at all. A value of 00 means the color won't be shown at all and everything behind it will be visible.

  • R - Red value; self-explanatory

  • G - Green value; self-explanatory

  • B - Blue value; self-explanatory

What you entered in hex is 0x016569C1 which has an Alpha values of 1 (barely visible). Put, 0xFFFF0000 and you'll have a red background.

like image 151
DeeV Avatar answered Dec 16 '22 06:12

DeeV


You are passing in the color incorrectly. DeeV got to it before me but you need to use a hex value.

Here is a link that lists all combinations for easy access.

Colors for Android

You can also set in the XML by using

android:background = "#FF00000000"

Which would be black.

like image 44
sealz Avatar answered Dec 16 '22 05:12

sealz