Search code examples
androidandroid-layoutfloating-action-button

How to bring the FAB to the top of the layout


I am trying to have 3 FAB's between two views. But the FAB's are below the top view as shown below.

How do I bring them to the front?

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:clickable="true"
    android:focusableInTouchMode="true"
    android:id="@+id/parentPanel"
    android:background="#ffffff">

    <LinearLayout
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">

        <android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            card_view:cardCornerRadius="6dp"
            android:layout_margin="10dp"
            android:clickable="true"
            android:focusable="true"
            android:foreground="?android:selectableItemBackground"
            android:id="@+id/deviceCard">

            <fragment
                android:id="@+id/map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="io.tegaru.beepr.AddNewMapsLocationActivity" />

        </android.support.v7.widget.CardView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottomPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="4"
        android:background="@android:color/white">

        <LinearLayout
            android:id="@+id/btnPanel1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:background="@android:color/white">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:onClick="AddNewReminderBtnClicked"
                app:backgroundTint="#292929"
                app:fabSize="normal"
                app:srcCompat="@drawable/ic_menu_send"
                android:layout_gravity="top|center"
                android:layout_marginTop="-20dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/btnPanel2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:background="@android:color/white">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:onClick="AddNewReminderBtnClicked"
                app:backgroundTint="#292929"
                app:fabSize="normal"
                app:srcCompat="@drawable/ic_menu_send"
                android:layout_gravity="top|center"
                android:layout_marginTop="-20dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/btnPanel3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:background="@android:color/white">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:onClick="AddNewReminderBtnClicked"
                app:backgroundTint="#292929"
                app:fabSize="normal"
                app:srcCompat="@drawable/ic_menu_send"
                android:layout_gravity="top|center"
                android:layout_marginTop="-20dp" />
        </LinearLayout>


    </LinearLayout>

</LinearLayout>

Solution

  • To space the fabs equally and use them as an overlay for a web view for example, look at my code:

    enter image description here

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">
    
        <WebView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"></WebView>
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:clickable="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/fab2"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:clickable="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/fab3"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/fab1" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:clickable="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/fab2" />
    
    </android.support.constraint.ConstraintLayout>