Search code examples
androidandroid-layoutmaterial-designfloating-action-button

How to implement three floating action buttons next to each other (Android XML)


I'm putting big FloatingActionButton at center and I want to place 2 additional floating buttons next to the one in the center. Red rectangle in example pic. What option I can use to implement this?

enter image description here


Solution

  • You can do this in many way. Here is solution by using constraintlayout.

    activity_map.xml

    <androidx.constraintlayout.widget.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:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" android:id="@+id/linearLayout">
    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_gravity="bottom|end"
            app:elevation="6dp"
            android:background="@drawable/round_dark_gray"
            app:pressedTranslationZ="12dp" android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent" android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent"/>
    <TextView
            android:text="AR"
            android:textSize="28sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textStyle="bold"
            android:textColor="#FFFFFF"
            android:id="@+id/textView"
            app:layout_constraintTop_toTopOf="@+id/fab" app:layout_constraintStart_toStartOf="@+id/fab"
            app:layout_constraintBottom_toBottomOf="@+id/fab" app:layout_constraintEnd_toEndOf="@+id/fab"
    />
    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="bottom|end"
            app:elevation="6dp"
            android:background="@drawable/round_dark_gray"
            app:pressedTranslationZ="12dp" android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginEnd="8dp" app:layout_constraintEnd_toStartOf="@+id/fab"/>
    
    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab3"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="bottom|end"
            app:elevation="6dp"
            android:background="@drawable/round_dark_gray"
            app:pressedTranslationZ="12dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginBottom="8dp"
            app:layout_constraintStart_toEndOf="@+id/fab" android:layout_marginStart="8dp"/>
    <TextView
            android:text="P"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            android:textSize="25sp"
            android:textStyle="bold"
            android:textColor="#FFFFFF"
            app:layout_constraintTop_toTopOf="@+id/fab2" app:layout_constraintBottom_toBottomOf="@+id/fab2"
            app:layout_constraintEnd_toEndOf="@+id/fab2" app:layout_constraintStart_toStartOf="@+id/fab2"/>
    <TextView
            android:text="A"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView3"
            android:textSize="25sp"
            android:textStyle="bold"
            android:textColor="#FFFFFF"
            app:layout_constraintTop_toTopOf="@+id/fab3" app:layout_constraintBottom_toBottomOf="@+id/fab3"
            app:layout_constraintStart_toStartOf="@+id/fab3" app:layout_constraintEnd_toEndOf="@+id/fab3"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    create round_dark_gray.xml file in res->drawable folder

    <?xml version="1.0" encoding="utf-8"?>
     <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="@android:color/darker_gray"/>
     </shape>
    

    you can create background drawable as per your requirement.