Search code examples
androidpositionmarginandroid-relativelayoutfloating-action-button

Floating Action Button layout_margin(Bottom) has no effect on bottom margin


I am testing on an Android 6.0.1 device and the problem is that my FAB only reacts to layout_marginRight and not layout_marginBottom no matter which value I am using.

Here is my layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:id="@+id/scrollViewUnitNames"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:layout_margin="16dp"
    android:fillViewport="true"><!----></ScrollView>

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_margin="@dimen/fab_margin"
    android:clickable="true"
    android:id="@+id/fab"
    android:src="@android:drawable/ic_dialog_email"
    app:fabSize="normal"
    app:borderWidth="0dp"
    />      </RelativeLayout>

So if I align the FAB to the top left or top right corner, the margin works. If I align it to the Bottom left or right corner, layout_marginBottom seems to have no effect. I am using the design support library 23.2.0. I tested it on an Android 4.0.3 emulator and it worked. Do you have any ideas how to fix this? Thanks!

Edit this is what it looks like: enter image description here

Edit 2 I now know a little bit more: on create, I set the content view to an empty layout (only a relative layout inside). After some tests I change the layout like this:

RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
container.removeAllViews();
container.addView(getLayoutInflater().inflate(R.layout.my_layout_file, null));

In a new project, I did the same and before I changed the layout it worked and after I changed it, it looked the same like on the image above. Is this an uncorrect way to change the layout? Or is there a better method? Thanks for your help.


Solution

  • Change the xml for your FloatingActionButton to something like this:

    <android.support.design.widget.FloatingActionButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_alignParentBottom="true"
          android:layout_marginLeft="@dimen/fab_margin"
          android:layout_marginTop="@dimen/fab_margin"
          android:layout_marginRight="@dimen/fab_margin"
          android:layout_marginBottom="50dp"
          android:clickable="true"
          android:id="@+id/fab"
          android:src="@android:drawable/ic_dialog_email"
          app:fabSize="normal"
          app:borderWidth="0dp"
          />
    

    If you use android:layout_margin and android:layout_marginBottom, it will overwrite your bottom margin. Defining each margin by itself will allow you to have a different bottom margin than top/left/right.

    Updated answer:

    Change this line:

    container.addView(getLayoutInflater().inflate(R.layout.my_layout_file, null));
    

    To

    container.addView(getLayoutInflater().inflate(R.layout.my_layout_file, container, false));
    

    And it should correct the margin on the FloatingActionButton.