Search code examples
androidandroid-coordinatorlayoutfloating-action-button

Container for Floating Action Button + Snackbar


I have this hierarchy:

<CoordinatorLayout>
  <Framelayout/>
  <NestedScrollView>
    <LinearLayout>
      <FrameLayout/>
    </LinearLayout>
  </NestedScrollView>
  <FloatingActionButton>
</CoordinatorLayout>

I call my snackbar with the root view. With this layout my snackbar works perfectly as i wanted.

How can i achieve that i can load the FloatingActionButton dynamically and wrap it into a container?

I tried to wrap it into another CoordinatorLayout. So the layout looked like this:

<CoordinatorLayout>
  <Framelayout/>
  <NestedScrollView>
    <LinearLayout>
      <FrameLayout/>
    </LinearLayout>
  </NestedScrollView>
  <CoordinatorLayout> <!-- This is the added container -->
    <FloatingActionButton> <!-- I would like to load it dynamically from another layout file -->
  <CoordinatorLayout>
</CoordinatorLayout>

The problem with this is that when i call my snackbar it overlaps the FloatingActionButton.

I also tried it with a LinearLayout or RelativeLayout instead but then the FloatingActionButton wasn't be placed correctly.


Solution

  • In my case i load a content layout inside a class programmatically and don't know on the root layout (the defined xml layout) whether a FloatingActionButton will exist. I added the FloatingActionButton inside the content layout but it didn't work correctly when Snackbar slide up. I tried using https://github.com/natario1/NestedScrollCoordinatorLayout but it didn't work for me. Solution for me is to add the FloatingActionButton programmatically to the root CoordinatorLayout. Then it's a direct child of CoordinatorLayout and works correctly. Didn't find a solution where i can have nested CoordinatorLayout which work correctly.