nispok/snackbar

Up FloatButton when it shows the message

Opened this issue · 7 comments

Congratulations on your fantastic library.

I have the following situation.

I have a layout where it has a RecyclerView and FloatButton.
Can you explain your blibioteca, the message appears over my FloatButton and correct it would raise the FloatButton.

Know how I can fix this?

thank you.

wmora commented

Hi, there's an EventListener that will let you know when the Snackbar will show up through the onShow method. When this is called you should raise your FloatButton. Similarly, you should bring your button back down when onDismiss is called

Hi wmora,

I am having the same issue, due to a floating action button.

Unfortunately simply raising and lowering the FAB in the EventListener doesn't seem to work for me.

Here is my code. The FAB is a child of rlContent, positioned at bottom right.

private class SnackbarListener implements EventListener {

    @Override
    public void onShow(Snackbar snackbar) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
        );
        params.setMargins(0, 0, 0, snackbar.getHeight());
        rlContent.setLayoutParams(params);
    }

    @Override
    public void onShowByReplace(Snackbar snackbar) {
    }

    @Override
    public void onShown(Snackbar snackbar) {
    }

    @Override
    public void onDismiss(Snackbar snackbar) {
    }

    @Override
    public void onDismissByReplace(Snackbar snackbar) {
    }

    @Override
    public void onDismissed(Snackbar snackbar) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
        );
        rlContent.setLayoutParams(params);
    }
}

This would work in normal cases. But if a second snackbar is shown before the previous one is dismissed, when the first snackbar disappears, onDismissed is called, and the margin will be incorrectly set to 0, i.e. FAB not raised.

Currently my workaround is to maintain a snackbar counter in the activity. The counter is increased by one whenever a snackbar is shown, and in onDismissed, the margin to reset to 0 only if the counter becomes zero.

But to me my solution is not so elegant. Is there a better way to deal with FABs?

I figured it out... Just reset the margin to 0 in onDismiss instead of onDismissed
I guess onDismissed is called after either onDismiss or onDismissByReplace

Still, the movement of the FAB is not as smooth as the demo in http://www.google.com/design/spec/components/snackbars-toasts.html#snackbars-toasts-usage

gedu commented

Hi zhangns, for make it a bit smoother i use this:
onShow(int i){
fabView.animate().translationY(-snackbarHeight).start();
}
and
onDismiss(int i){
fabView.animate().translationY(0).start();
}

hope this helps

@gedu Thanks! Works perfectly.

@gedu your solution works. At first I was attempting to add an Interpolator to fabView but couldn't get the animation on both fabView and snackbar to synchronize