Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve Floating Action Button in Codenameone?

Floating Action button in android is good option. I want this in my codenameone application. I have tried it by using LayeredLayout, by having two layouts. I'm unable to achieve it perfectly. The button is keep moving along with the scroll. how to fix the button to the right bottom of the screen without affecting when the background layer is scrolling.

Here is how i tried.

Form myForm = new Form();
myForm.setLayout(new LayeredLayout());
myForm.setTitle("Floating Action Button");

SpanLabel lbl = new SpanLabel("some long text");

Container conBottom = new Container();
conBottom.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
conBottom.addComponent(lbl);

FlowLayout flow = new FlowLayout(Component.RIGHT);
flow.setValign(Component.BOTTOM);
Container conUpper = new Container(flow);
conUpper.addComponent(new Button("+"));
conUpper.setScrollable(false);

myForm.addComponent(conBottom);
myForm.addComponent(conUpper);

myForm.show();

Here is the link similar to what i want to achieve. https://github.com/Clans/FloatingActionButton Please Help! Thanks!

like image 924
mahmood sk Avatar asked Oct 26 '15 12:10

mahmood sk


People also ask

How do you implement a floating action button?

Add the floating action button to your layoutThe size of the FAB, using the app:fabSize attribute or the setSize() method. The ripple color of the FAB, using the app:rippleColor attribute or the setRippleColor() method. The FAB icon, using the android:src attribute or the setImageDrawable() method.

How do you make a floating action button invisible?

To show and hide a FloatingActionButton with the default animation, just call the methods show() and hide() .


2 Answers

The Form's content pane is performing the scrolling, you need your bottom container to handle the scrolling instead. Try this:

    Form myForm = new Form();
    myForm.setLayout(new LayeredLayout());
    myForm.setTitle("Floating Action Button");

    SpanLabel lbl = new SpanLabel("some long text ");

    Container conBottom = new Container();
    conBottom.setScrollableY(true);
    conBottom.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    conBottom.addComponent(lbl);

    FlowLayout flow = new FlowLayout(Component.RIGHT);
    flow.setValign(Component.BOTTOM);
    Container conUpper = new Container(flow);
    conUpper.addComponent(new Button("+"));
    conUpper.setScrollable(false);

    myForm.addComponent(conBottom);
    myForm.addComponent(conUpper);
    myForm.setScrollable(false);
    myForm.show();
like image 96
Chen Avatar answered Sep 30 '22 16:09

Chen


Another way to achieve this is by placing the button on the form LayeredPane. This allows you to customise your form layout to handle anything you want. With this, you don't have to set your form to LayeredLayout. Here is a code you can use to achieve that:

    FlowLayout fl = new FlowLayout(Component.RIGHT);
    fl.setValign(Component.BOTTOM);
    Container cont = new Container(fl);
    Button btn = new Button("+");
    //OR
    //Button btn = new Button(getImageFromTheme("plus_icon.png"));
    btn.addActionListener(null);
    btn.setUIID("ButtonFloat");
    cont.addComponent(btn);
    myForm.getLayeredPane().addComponent(cont);
    myForm.getLayeredPane().setLayout(new LayeredLayout());

    btn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
           //Handle your btn click here
        }
    });
like image 40
Diamond Avatar answered Sep 30 '22 16:09

Diamond