Why is my Android background color not showing?

I am trying to understand one simple thing: how to set the background color in the Android view. Here is the code in Office:

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View v = new View(this); setContentView(v); v.setBackgroundColor(23423425); } 

and all i get is a black screen.

+4
source share
3 answers

The integer that you specify is easier to represent as a hexadecimal value. Hex values ​​are 0xAARRGGBB .

  • A - represents the Alpha value, which is a transparent color. A value of FF means that it is not transparent at all. A value of 00 means that the color will not be displayed at all, and everything behind it will be visible.

  • R is the red value; self explanatory

  • G is the value of green; self explanatory

  • B - Blue value; self explanatory

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

+25
source

You specify the color incorrectly. DeeV got to it, but you need to use a hex value.

Here is a link listing all combinations for easy access.

Colors for Android

You can also install in XML with

 android:background = "#FF00000000" 

What will be black.

+3
source

The usual way to represent colors in ARGB (sometimes RGBA, but that's just a name) is in hex. No one uses a decimal number system to represent color digitally.

set yellow color on the button text: button.setTextColor(0xFFFFFF00); . Now we set the yellow button text.

ARGB consists of 4 channels. each with 8 bits. first channel - alfa - 0x FF FFFFFF; alfa - opacity level (in this case we have the maximum value). the second is red - 0xFF FF FF00, etc .; green and blue respectively.

The easiest way to create a color in an ARGB color model with a decimal number system is to use the Color class.

Color class has all the basic static functions and fields. In your case, you can use the static function Color.rgb(int red, int, green, int blue) , where red, green, blue should be in the range from 0 to 255. Alfa bits are set by default to max - 255 or in hex - 0xff.

Now that you know how to represent a color in a hexadecimal number system, it will be very easy to create color in an xml resource file.

+2
source

All Articles