nambicompany / expandable-fab

A highly customizable 'speed dial' FloatingActionButton implementation.
https://nambicompany.github.io/expandable-fab/
MIT License
200 stars 21 forks source link

How to add a fab option programatically? #11

Closed osfunapps closed 3 years ago

osfunapps commented 3 years ago

Hey,

I have, in my XML:

  <com.nambimobile.widgets.efab.ExpandableFabLayout
        android:id="@+id/fab_layout"
        android:layout_width="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_margin="12dp"
        android:layout_height="wrap_content">

    <!-- The next 3 Views will only display in portrait orientation -->
    <com.nambimobile.widgets.efab.ExpandableFab
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:efab_orientation="portrait"/>
    </com.nambimobile.widgets.efab.ExpandableFabLayout>

And I'm trying, via code, to add the option like so:

 /// will add the keyboard capability
private fun addKeyboardCapability(fabLayout: ExpandableFabLayout) {
     val option = FabOption(fabLayout.context, Orientation.PORTRAIT)
     option.label.text = "keyboard"
     fabLayout.addView(option, fabLayout.childCount, null)
     }

But the option isn't scaled properly on screen, probably because I'm not adding the option right or something.

I can refresh myself the ExpandableFabLayout but I thought that maybe I'm not adding the FabOption right.

kabumere commented 3 years ago

Hey @osfunapps,

To add a FabOption programmatically, call the addView variant that only takes in the single child View to add (or call the addViews method, which takes in zero or more children). Don't bother dealing with the overloaded addView variant that also accepts the index and LayoutParams, as you use in your example. The platform will handle that for you. So your code should look like:

fabLayout.addView(option) or fabLayout.addViews(option) (note the s in the 2nd code example).

Please note that the added FabOption will be appended to the end of the list (so it will be at the top if FabOptionPosition on your ExpandableFab is set to ABOVE or at the bottom if FabOptionPosition is set to BELOW). At this time, the library does not allow you to set the specific index within the list of FabOptions for a given ExpandableFabLayout.

kabumere commented 3 years ago

Hey @osfunapps, did the above help solve your issue?

osfunapps commented 3 years ago

Thanks for your help.

unfortunately that didnt solve my problem. The Option is still seen only half way.

I used a very old fab library instead.