1

I am trying to implement a BottomAppBar into my app and can't make the FAB or BottomAppBar display at the bottom. Here is my layout:

<?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"
tools:context=".dashboard.DashboardActivity">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add_white_24dp"
    app:layout_anchor="@id/bottom_bar"/>

<android.support.design.bottomappbar.BottomAppBar
    android:id="@+id/bottom_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/mtrl_bottomappbar_height"
    android:layout_gravity="bottom"
    app:backgroundTint="@color/colorPrimary"
    app:fabAttached="true"
    app:fabCradleVerticalOffset="12dp"
    app:fabAlignmentMode="center"/>

</android.support.constraint.ConstraintLayout>

However, this is what is being shown in the emulator:

Image

How do I align it at the bottom?

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Charlie Niekirk
  • 1,015
  • 1
  • 10
  • 15

3 Answers3

1

Sometimes it depends on your root layout too.
for ConstraintLayout try app:layout_constraintBottom_toBottomOf="parent"

Check this answer for other layouts too.

Man
  • 2,720
  • 2
  • 13
  • 21
1

ConstraintLayout doesn't make use of layout_gravity. Instead, use:

app:layout_constraintBottom_toBottomOf="parent" 

without margin.

HaroldSer
  • 2,025
  • 2
  • 12
  • 23
  • Thanks, I actually ended up wrapping my FAB and BottomAppBar in a CoordinatorLayout instead. Using your snippet worked for the BottomAppBar but the FAB would not attach whatever I tried. – Charlie Niekirk Jun 24 '18 at 19:39
1

Firstly, change the layout to LinearLayout and define orientation as vertical in it and then put your code in another LinearLayout in which you can make its marginTop as your requirements just make these changes it should work fine

<LinearLayout
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=".dashboard.DashboardActivity"
android:orientation="vertical"

>

<LinearLayout
   android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="330dp" //mark height accordingly
 >


  \\put your code here



</LinearLayout>


</LinearLayout>

try this should work fine for you

Avinash jain
  • 486
  • 1
  • 7
  • 15