Search code examples
javaandroidandroid-layoutmaterial-designandroid-coordinatorlayout

Why is Coordinate layout ( CL )hiding half of the floating action button when the height of CL is set to wrap_Content?


So I am trying to make a layout like this :

enter image description here

One way to do that is to use a bottom app bar Anatomy, but the thing with bottom app bar is that it can be only used within a coordinate layout. But I want to have a listview or say any other views above it so I am using a constraint layout and then adding coordinate layout with bottom app bar and fab, the thing is that when I set the height of coordinate layout to wrap content in order to make space above it for other views it hides half of the FAB { floating action button ) but on setting coordinate layout height to match_parent shows the full FAB, now when I'll use match_parent as height it will replace constraint layout and I'll be no longer be able to normally use constraint layout for adding other views, so how can I make the height of constraint layout to wrap_content while showing the complete FAB

Here is the code [XML] which I want to use but is not producing desired results as it hiding half of the FAB

<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"
    android:background="@color/colorAppWhite"
    tools:context=".ActivityFive">


    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent">

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/af_bn"
            style="@style/Widget.MaterialComponents.BottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:backgroundTint="@color/colorWaarkDB1"
            app:fabAlignmentMode="center"
            app:fabCradleRoundedCornerRadius="16dp">

        </com.google.android.material.bottomappbar.BottomAppBar>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_add_black_24dp"
            app:elevation="16dp"
            app:fabSize="normal"
            app:layout_anchor="@+id/af_bn" />


    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

It gives me something like this (1st) :

enter image description here

but I want this ( 2nd ):

https://media.discordapp.net/attachments/597356915219103745/617762091134222366/fab1.png?width=165&height=300

for achieving something like image 2 I'' have to set the height of coordinatorlayout to match_parent but I want the coordinator layout to just be as big as the space taken by it's children, how can I do that ?


Solution

  • Try something like below... I had similar case and this one helped me..

    <?xml version="1.0" encoding="utf-8"?>
    <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"
    tools:context=".controller.MainActivity">
    
    <include layout="@layout/content_listview_here"/>
    
    <android.support.design.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        app:backgroundTint="#E3E3E3"
        app:hideOnScroll="false"
        app:fabAlignmentMode="center"/>
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_home_white"
        app:backgroundTint="@color/colorPrimary"
        app:fabSize="normal"
        app:layout_anchor="@id/bottomAppBar" />
    
    </android.support.design.widget.CoordinatorLayout>