Search code examples
androidandroid-coordinatorlayoutfloating-action-button

How to align a FAB on top of a Layout border


I am trying to create the below design where there are 2 FABs and a rectangular layout. I can align the FABs on various positions but not exactly as in the image. Using app:layout_anchorGravity="top|right|end" I get it exactly at the corner, but the one in the image is slightly below the top-right corner, and the one in the left is above the bottom-left corner. enter image description here

Below is my xml file, it only includes one FAB. It is easy to implement the second FAB if I manage to do the first one:

<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">


<LinearLayout
    android:id="@+id/myLayout"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="horizontal"
    android:layout_margin="100dp"
    android:background="#403AA0F5">

</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_marginTop="60dp"
    android:clickable="true"
    app:layout_anchor="@id/myLayout"
    app:layout_anchorGravity="top|right|end"/>

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

Solution

  • You can create this layout using constraint layout. Change your measurements accordingly. I gave the rectangle width of 200dp and height of 100dp. Here is the code:

    <android.support.constraint.ConstraintLayout 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">
    
    
    <View
        android:id="@+id/myLayout"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:background="#403AA0F5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginBottom="60dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="@+id/myLayout"
        app:layout_constraintEnd_toEndOf="@+id/myLayout"
        app:layout_constraintHorizontal_bias="0.607"
        app:layout_constraintStart_toEndOf="@+id/myLayout" />
    
    <android.support.design.widget.FloatingActionButton
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/myLayout"
        app:layout_constraintEnd_toStartOf="@+id/myLayout"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/myLayout" />
    
    </android.support.constraint.ConstraintLayout>
    

    Here is the small snippet of the design:

    enter image description here