Fahd Lihidheb
Fahd Lihidheb

Reputation: 710

Codename one container background color

I am trying to change the background color for a specific container with this line of code :

Container container = new Container(new BorderLayout());
container.getStyle().setBgColor(0x99CCCC);

but nothing happens, i used also repaint() but also nothing. the same with setBgTransparency(0)

Upvotes: 4

Views: 1957

Answers (4)

steve hannah
steve hannah

Reputation: 4716

The Component background can be tricky. Some things to consider:

  1. If the style has an image border defined, then that will take precedence over any other background settings.
  2. If the style has an image background, then that will take precedence over BgColor()
  3. If the style's BgTransparency() is set to 0 then it doesn't matter what bgcolor you set, you won't be able to see it.

So, to cover all bases, you might do something like:

myComponent.getAllStyles().setBorder(Border.createEmpty());
myComponent.getAllStyles().setBackgroundType(BACKGROUND_NONE);
myComponent.getAllStyles().setBgTransparency(255);
myComponent.getAllStyles().setBgColor(myColor);

Or, using the fluent API of the ComponentSelector class:

$(myComponent)
    .setBorder(Border.createEmpty())
    .setBackgroundType(BACKGROUND_NONE)
    .setBgTransparency(255)
    .setBgColor(myColor);

Upvotes: 2

Poly Hamza
Poly Hamza

Reputation: 159

In CodeNameOne three steps to have a gradient colored container: 1. getUnselectedstyle 2. setBackgroundType : that can be either : BACKGROUND_GRADIENT_LINEAR_HORIZONTAL BACKGROUND_GRADIENT_LINEAR_VERTICAL ...

  1. setBackgroundGradientStartColor and EndColor (if you wish to have no gradient you should make same color for StartColor and EndColor)

                Container Container1 = new Container();
                Container1.getUnselectedStyle().setBackgroundType(Style.BACKGROUND_GRADIENT_RADIAL);
                Container1.getUnselectedStyle().setBackgroundGradientEndColor(0xFFBCCA);
                Container1.getUnselectedStyle().setBackgroundGradientStartColor(0xFFBCCA);
    

Upvotes: 1

tizbn
tizbn

Reputation: 1907

setBgTransparency(0) make container to transparent so setBgTransparency to 255 to make it opaque . And hope the following codes will help you

Container container = new Container(new BorderLayout());
container.getStyle().setBgColor(0x99CCCC);
container.getStyle().setBgTransparency(255);

Upvotes: 3

Gaurav Takte
Gaurav Takte

Reputation: 615

If you want to format the container or change style of the container, then you just have to create UIID in designer for container, Here you can format background color, margin, padding, etc. So you just have to create UIID and apply it to specific container.

For example:-

Container container = new Container();
container.setUIID("Container_uiid_name");

and you achieve the expected output.

Upvotes: 2

Related Questions