How to center a container?

36 views
Skip to first unread message

Compte Gmail

unread,
May 19, 2021, 10:36:21 PM5/19/21
to CodenameOne Discussions
Having this problem. You'll find the display below,  please how to center it? 

CODE:    


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

   Button concert=new Button("Concerts");

   Button play=new Button("Theatres");
   Button movie=new Button("Movies");
   Button cinema=new Button("Cinemas");
    Button musicien=new Button("Musiciens");
     Button tactor=new Button("Theatre Actor");
      Button factor=new Button("Movie Actor");
         Button logout=new Button("Logout");
   f1.addAll(concert,play,movie,cinema,musicien,tactor,factor,logout);
        add(f1);


OUTPUT
test.png

P5music

unread,
May 20, 2021, 2:49:30 AM5/20/21
to CodenameOne Discussions
Maybe you should use BorderLayout so you can place the f1 container in the center of it.

Container f1 =  new Container(BoxLayout.y()); 
Container blContainer =  new Container(new BorderLayout());

   Button concert=new Button("Concerts");

   Button play=new Button("Theatres");
   Button movie=new Button("Movies");
   Button cinema=new Button("Cinemas");
    Button musicien=new Button("Musiciens");
     Button tactor=new Button("Theatre Actor");
      Button factor=new Button("Movie Actor");
         Button logout=new Button("Logout");
   f1.addAll(concert,play,movie,cinema,musicien,tactor,factor,logout);

blContainer.add(BorderLayout.CENTER,f1);
        add(blContainer);

I do not test it but I have some similar code in my app. It should work.

Regards

Shai Almog

unread,
May 21, 2021, 12:53:18 AM5/21/21
to CodenameOne Discussions
You can use several strategies but the simplest one would be to use TableLayout roughly like this:

TableLayout tl = new TableLayout(NUMBER_OF_BUTTONS, 1);
Container f1 =  new Container(tl);
f1.add(tl.cc().ha(CENTER).wp(100), play);
f1.add(tl.cc().ha(CENTER), movie);

Notice the other lines don't need the widthPercent value since they're in the same column.
Reply all
Reply to author
Forward
0 new messages