1
votes

i want to add a background color to my container i tried everything but it's not working i didn't know why

 Container c2 = new Container(BoxLayout.y());

    for (int i = 0; i < lis.size(); i++) {
        Container c1 = new Container(BoxLayout.y());

        ImageViewer iv = new ImageViewer();

        iv.setImage(Image.createImage("/" + lis.get(i).getImage()).scaledHeight(100).scaledWidth(100));
        c1.add(iv);
        c1.add(new Label(lis.get(i).getNom()));

        c1.getUnselectedStyle().setBorder(Border.createLineBorder(5));
        c1.getStyle().setBgColor(0xC40C0C);

        c2.add(c1);

    }
    f.add(c2);
1
The way codename1 containers are sized and drawn is quite complex and opaque. Subclass Container, give it a paint(Graphics ) wrapper method, and set a breakpoint. Check the size of the window, you might be surprised to find it's 0x0. After calling super() draw some lines and boxes you should be able to see. Likewise for ImageViewer. good luck! - ddyer
@ddyer i really didn't understand what u told me to do well can u please be more precise or if u can show me an example ? - AOUADI Slim
I told you how I would debug the problem. I'm afraid if you don't understand, you're not ready to do what you're doing, or you need a better teacher than me. - ddyer

1 Answers

0
votes

Border overrides background color which can impact the background. Also you will need to set the background transparency which in container is 0 e.g.:

c1.getUnselectedStyle().setBgTransparency(255);

I would suggest doing this in CSS or in the designer tool by defining a UIID to match your desired appearance then applying that using c1.setUIID("MyStyledBackground");.

You can have a border on top of the color by nesting containers.