views:

29

answers:

1

I'm using the follow code, to create a non-scaled, centred image as a background, in a relative layout:-

RelativeLayout explosionlayout = (RelativeLayout) findViewById (R.id.explosionlayout);
explosionlayout.setBackgroundColor(R.color.white);

Bitmap myBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.bomb);
    BitmapDrawable test1 = new BitmapDrawable(myBitmap);
    test1.setGravity(Gravity.CENTER);

The only problem I have, is that the background of the relativelayout is grey, regardless of what I set it to, either via XML or in code.

Any ideas would be appreciated, thanks.

A: 

You probably want this instead:

explosionlayout.setBackgroundColor(getResources().getColor(R.color.white));

or just

explosionlayout.setBackgroundColor(0xffffffff);

The reason is that R.color.white is an ID, while setBackgroundColor expects an actual 32-bit integer representation of the color.

Roman Nurik
R.color.white is set in the res/values/color.xml file and is defined with the following line. <color name="white">#FFFFFF</color>
andy_spoo
Right, but R.color.white is not a real color value, it's just an ID. `setBackgroundColor` requires a real color value, which `Resources.getColor` will provide.
Roman Nurik