Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mimic Snackbar + CoordinatorLayout + FAB with new MotionLayout

Let's say we have a BottomNavigationBar with a FAB on top. When using this setup and showing a Snackbar, the Snackbar should appear above the BottomNavigationBar and push up/down the FAB while appearing/dismissing.

This is a common scenario for the use of CoordinatorLayout. Is it also possible to create this animation using the new MotionLayout?

There is an issue in the bug tracker, but it's already closed, because OP didn't clearly formulate the question (https://issuetracker.google.com/issues/112665540)

Problems I ran into while trying: We don't have access to the layout-id of the Snackbar. We also don't have access to the general xml of the Snackbar, so we cannot set its Constraints.

UPDATE: I understand that a piece of code would help as a starting point to answer this question. But what ever piece of code I came up with yet, was not useful at all. I pinpointed now the 2 main problems:

  1. I do not know the layout-id of the Snackbar. Therefore I cannot use it in writing a Scene description.

  2. Even if I could create a Scene description (e.g. State1: SnackBar visible, State2: Snackbar not visible). I'd have to trigger these Scene transitions by hand. That means that I would re-create my own Snackbar instead of using the original Snackbar as it is intended.

COMMENTS: @mikejonesguy Yes, in my opinion they are similar. Not the same, but similar. And MotionLayout does also have the job you described: to coordinate interactions among its subviews. Replace "coordinate" with "animate" and you'll see my point. Also have a look at the OnSwipe/OnClick handlers. MotionLayout is still very fresh, but I think it will replace CoordinatorLayout in the future the same way ConstraintLayout replaced RelativeLayout. Maybe I'm wrong, maybe not...time will tell.

CONCLUSION: As far as I'm concerned, there seems to be no way (yet) to achieve what I want only by using MotionLayout. If it's possible one day, I'll update this question with a working example. Sorry for the impossible bounty... :)

like image 759
muetzenflo Avatar asked May 11 '19 09:05

muetzenflo


People also ask

How to use coordinatorlayout in the demo app?

The demo app, available on github, has a MainActivity, with four buttons, for each of the different ways of using CoordinatorLayout we discuss below. 1. Snackbar and FAB For the first activity, we want to build a layout with a FAB that automatically slides out of the way, when a Snackbar is displayed.

How to inform motionlayout about the progress of the collapsing toolbar?

Every time the Collapsing Toolbar should change its collapsed progress, it will inform us by just converting `verticalOffset` to a state of animation between 0f and 1f. It will allow us to inform MotionLayout about the new progress.

What is the difference between coordinatorlayout and framelayout?

If you have used a FrameLayout before, you should be very comfortable using CoordinatorLayout. If you haven’t used a FrameLayout, do not worry, it is pretty straightforward. By default, if you add multiple children to a FrameLayout, they would overlap each other. A FrameLayout should be used most often to hold a single child view.


1 Answers

You can design your layout files as if the snackbar is in your layout hierarchy. You need to make use of Constraintlayout's virtual helper objects. Virtual objects are invisible but they act as a regular view during measuring & layout, therefore their purpose is to help you create the exact positioning you want.

A snackbars default height is min 48dp and max 80dp. You are going to need this information.

like image 113
Nezih Yılmaz Avatar answered Nov 10 '22 23:11

Nezih Yılmaz