Search code examples
androidmarginfloating-action-button

Floating Action Button Bottom-Margin doesn't work


I tried to implement a Floating Button on the bottom-right of the screen, but the bottom-margin doesn't work for some reason :/ I tried to change the margin size only on the Bottom but it didn't work.

Layout Files:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.js_labs.gaminggroups.MainActivity"
android:id="@+id/appBarMainRl">

<include
layout="@layout/some_content" // Changed by LayoutInflater in Java ...
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

some_content.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:useCompatPadding="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="50dp"
android:id="@+id/contentMyGroupsRl"
tools:context=".MainActivity">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/ic_action_addgroup"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    app:fabSize="normal"
    android:layout_margin="@dimen/fab_margin"
    android:layout_alignParentBottom="true" />

</RelativeLayout>

Screenshot: https://i.sstatic.net/COpgF.png


Solution

  • First, try to remove this line

    android:layout_alignParentBottom="true"
    

    from FloatingActionButton

    Second Step, move FloatingActionButton from Relative layout to CoordinatorLayout itself, as a direct child.

    Should look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="de.js_labs.gaminggroups.MainActivity"
    android:id="@+id/appBarMainRl">
    
    <include
    layout="@layout/some_content" // Changed by LayoutInflater in Java ...
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_action_addgroup"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        app:fabSize="normal"
        android:layout_margin="@dimen/fab_margin"
    />
    
    </android.support.design.widget.CoordinatorLayout>