2

I am using the code below as the item layout in RecyclerView, the FloatingActionButton should be located in the bottom-end corner, but it appears on the top-start sometimes, I want to figure it out why this happening...

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="@dimen/record_task_card_width"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/record_task_card_margin"
    android:clickable="true"
    android:foreground="?attr/selectableItemBackground">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <FrameLayout
                android:id="@+id/card_background_container"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:background="@drawable/bg_card">

                <ImageView
                    android:id="@+id/record_task_icon"
                    android:layout_width="@dimen/record_task_icon_size"
                    android:layout_height="@dimen/record_task_icon_size"
                    android:layout_gravity="center"
                    android:contentDescription="@string/app_name"/>

            </FrameLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/record_task_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/record_task_info_margin"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title"/>

                <TextView
                    android:id="@+id/record_task_caption"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/TextAppearance.AppCompat.Caption"/>

            </LinearLayout>

        </LinearLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/start_record_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/fab_margin"
            android:clickable="true"
            app:layout_anchor="@id/card_background_container"
            app:layout_anchorGravity="bottom|end|right"
            app:srcCompat="@drawable/ic_start_record"/>

    </android.support.design.widget.CoordinatorLayout>

</android.support.v7.widget.CardView>
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Guomato
  • 91
  • 1
  • 6

1 Answers1

3

Remove

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

intead add

android:layout_gravity="bottom|end" 

Sample:

<android.support.design.widget.FloatingActionButton
            android:id="@+id/start_record_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/fab_margin"
            android:clickable="true"
            android:layout_gravity="bottom|end"
            app:srcCompat="@drawable/ic_start_record"/>
SaravInfern
  • 3,338
  • 1
  • 20
  • 44