givi
givi

Reputation: 1903

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.

Upvotes: 4

Views: 14379

Answers (3)

DeeV
DeeV

Reputation: 36045

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.

Upvotes: 24

Beglarm
Beglarm

Reputation: 58

Common way to represent color in ARGB(sometimes RGBA but it is just a naming) model is hexadecimal. No one uses decimal numeral system to represent color digitally.

let's set yellow color to button's text: button.setTextColor(0xFFFFFF00);. Now We set yellow to out button's text.

ARGB consists of 4 cannel. each with 8-bit. first channel is alfa - 0xFFFFFFFF; alfa is opacity level(in this case we have max value of it). second is red - 0xFFFFFF00, and so on; green and blue respectively.

The easiest way to create color in ARGB color model with decimal numeral system is to use Color class.

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

Now you know how to represent color in hexadecimal numeric system it will be very easy to create color in xml resource file.

Upvotes: 2

sealz
sealz

Reputation: 5408

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.

Upvotes: 3

Related Questions