Clans / FloatingActionButton

Android Floating Action Button based on Material Design specification
Apache License 2.0
5.23k stars 1.13k forks source link

Hide/Show button inside FabMenu #433

Closed ant-ia closed 6 years ago

ant-ia commented 6 years ago

Hi, what is the best way to hide and show button programmatically inside Menu? With android:visibility not work thanks

Croutonix commented 6 years ago

I have the same question, but there seem to be no easy way to do it. Here's what I did:

I have a flag for visibility, boolean fabShown. Then I set this as a menu click listener:

fabMenu.setOnMenuButtonClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (!fabMenu.isOpened()) fabTransfer.setVisibility(View.GONE);

        fabMenu.toggle(true);

        if (!fabMenu.isOpened() && fabShown) {
            fabTransfer.showButtonInMenu(true);
            }
    }
});

The only problem is the button in the menu is shown before the others, its animation is slightly shifted but this is barely noticeable.

EdwardvanRaak commented 4 years ago

For anyone still using this lib or one of its forks, these extensions I made worked for me, it uses the icon inside the primary FAB as the pivot point. As long as your implementation is basic, it should work as well.

fun FloatingActionMenu.show() {
    if (visibility != View.VISIBLE) {
        val pivotX = menuIconView.pivotX + menuIconView.x
        val pivotY = menuIconView.pivotY + menuIconView.y

        val anim = ScaleAnimation(
            0f, 1f, 0f, 1f, Animation.ABSOLUTE, pivotX,
            Animation.ABSOLUTE, pivotY
        )
        anim.duration = 200
        anim.interpolator = LinearInterpolator()
        startAnimation(anim)
    }
    visibility = View.VISIBLE
    close(false)
}
fun FloatingActionMenu.hide() {
    if (visibility == View.VISIBLE) {
        val pivotX = menuIconView.pivotX + menuIconView.x
        val pivotY = menuIconView.pivotY + menuIconView.y

        val anim = ScaleAnimation(
            1f, 0f, 1f, 0f, Animation.ABSOLUTE, pivotX,
            Animation.ABSOLUTE, pivotY
        )
        anim.duration = 200
        anim.interpolator = LinearInterpolator()
        startAnimation(anim)
    }
    visibility = View.INVISIBLE
    close(false)
}