Search code examples
androidandroid-constraintlayoutandroid-coordinatorlayout

How do I constrain components that are a direct children of CoordinatorLayout to components inside a ConstraintLayout?


I am trying to align a fab on the horizontal divider. At the same time I need the fab and CircularRevealLinearLayout to be direct children of the CoordinatorLayout. If they are not direct children, the fab will not animate. I tried to place the CoordinatorLayout within the ConstraintLayout and constrain it to the horizontal divider but it would not work.

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
    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:id="@+id/players_container"
    android:layout_width="500dp"
    android:layout_height="600dp">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.circularreveal.CircularRevealLinearLayout
            android:id="@+id/circular_reveal"
            android:layout_width="300dp"
            android:layout_height="500dp"
            android:elevation="10dp"
            android:visibility="invisible"
            app:layout_constraintTop_toTopOf="@id/edit_btn"
            app:layout_constraintEnd_toEndOf="@id/edit_btn"
            android:background="@color/color_secondary"
            app:layout_behavior="com.google.android.material.transformation.FabTransformationSheetBehavior"/>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/edit_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_edit_black_24dp"
            app:layout_constraintHorizontal_bias=".95"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/horizontal_divider"
            app:layout_constraintBottom_toBottomOf="@id/horizontal_divider"
            app:layout_constraintEnd_toEndOf="parent"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/image_layout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@id/horizontal_divider"
            app:layout_constraintTop_toTopOf="parent">


            <ImageView
                android:id="@+id/teamBackground"
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:src="@drawable/team_golfball"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/team_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="@id/image_layout"
                app:layout_constraintEnd_toEndOf="@id/image_layout"
                app:layout_constraintTop_toTopOf="@id/image_layout"
                app:layout_constraintBottom_toBottomOf="@id/image_layout"
                android:text="Team One"
                style="@style/TextAppearance.MyTheme.Headline7"/>

        </androidx.constraintlayout.widget.ConstraintLayout>


        <View
            android:id="@+id/horizontal_divider"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black_regular"
            app:layout_constraintTop_toBottomOf="@id/image_layout"
            app:layout_constraintBottom_toTopOf="@id/player_card_recyclerView"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/player_card_recyclerView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintHeight_percent=".75"
            android:overScrollMode="never"
            android:padding="20dp"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/horizontal_divider"
            tools:itemCount="5"
            tools:listitem="@layout/playercard_draggable" />

    </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</com.google.android.material.card.MaterialCardView>

enter image description here

what I want:

enter image description here


Solution

  • Please add -

    app:layout_anchor="@id/teamBackground"
    app:layout_anchorGravity="bottom|end"
    

    in the FloatingActionButton. layout_anchor is the anchor view that this view should position relative to. layout_anchorGravity is the position relative to an anchor, on both the X and Y axes, within its parent's bounds. Here I use bottom and end as you want to share your image.

    Full code will be -

    <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/edit_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_edit_black_24dp"
                app:layout_constraintHorizontal_bias=".95"
                app:layout_anchor="@id/teamBackground"
                app:layout_anchorGravity="bottom|end"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/horizontal_divider"
                app:layout_constraintBottom_toBottomOf="@id/horizontal_divider"
                app:layout_constraintEnd_toEndOf="parent"/>
    

    Please check out the layout_height of ImageView. It should be 0dp instead of 400dp.