Search code examples
androidxmlandroid-layoutfloating-action-button

Android Activity_Main Float Button


This is Acitivity_main.xml of a PDF Reader on Android.

I am confused on how to add two floating Action Buttons(Page Up/Down Button) in the bottom right corner. The buttons need to display above PDF, and uses linear vertical layout.

Following XML separates PDF and Buttons into two parts which is wrong, any idea how to correct it?

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/pdfstructure"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:orientation="vertical"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/DownButton"
        android:layout_width="100dp"
        android:layout_height="200dp"
        android:layout_gravity="end|bottom"
        android:layout_marginTop="600dp"
        android:layout_marginRight="30dp"
        android:clickable="true"
        android:contentDescription="@string/upButton"
        android:stateListAnimator="@null"
        android:text="Down"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/upButton"
    android:layout_width="100dp"
    android:layout_height="200dp"
    android:layout_gravity="end|bottom"
    android:layout_marginTop="800dp"
    android:layout_marginRight="30dp"
    android:clickable="true"
    android:text="Up"
    android:stateListAnimator="@null"
    android:contentDescription="@string/upButton"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Add this line in your linearLayout

    android:gravity="bottom"
    

    and delete those lines in your FloatingActionButton

    android:layout_marginTop="600dp"
    android:layout_marginTop="800dp"
    

    enter image description here