Nglhrdtptrck
Nglhrdtptrck

Reputation: 175

Position of second Floating Action Button isn't locked in layout

I have a layout with two CardViews. Each one has a FAB on its bottom right. POrtrait works fine, but in Landscape the FAB of the second CardView "breaks away" and scrolls with the layout. Do you know where my mistake is?

<RelativeLayout 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">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />


<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="48dp"
    android:orientation="vertical">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">


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

            <android.support.v7.widget.CardView
                android:id="@+id/cv2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/toolbar"
                android:layout_margin="20dp"
                android:layout_marginTop="50dp">

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

                    <TableLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TableRow>

                            <android.support.v7.widget.SwitchCompat
                                android:id="@+id/switch_persistent"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="20dp"
                                android:textOff=""
                                android:textOn="" />

                            <TextView
                                android:id="@+id/tv_persistent_title"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/persistent_reminder"
                                android:textSize="@dimen/abc_text_size_large_material" />

                        </TableRow>
                    </TableLayout>

                    <TextView
                        android:id="@+id/textView4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingBottom="15dp"
                        android:paddingTop="10dp"
                        android:text="@string/reminder"
                        android:textColor="@android:color/black"
                        android:textSize="@dimen/abc_text_size_large_material"
                        android:textStyle="italic" />

                </LinearLayout>
            </android.support.v7.widget.CardView>


            <android.support.v7.widget.CardView
                android:id="@+id/cv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/toolbar"
                android:layout_margin="20dp"
                android:layout_marginTop="50dp">

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

                    <TableLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TableRow>

                            <android.support.v7.widget.SwitchCompat
                                android:id="@+id/switch_startup"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="20dp"
                                android:textOff=""
                                android:textOn="" />

                            <TextView
                                android:id="@+id/tv_startup_title"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/startup_reminder"
                                android:textSize="@dimen/abc_text_size_large_material" />


                        </TableRow>

                    </TableLayout>


                    <TextView
                        android:id="@+id/tv_startup_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingBottom="15dp"
                        android:paddingTop="10dp"
                        android:text="@string/reminder"
                        android:textColor="@android:color/black"
                        android:textSize="@dimen/abc_text_size_large_material"
                        android:textStyle="italic" />

                </LinearLayout>
            </android.support.v7.widget.CardView>


        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_startup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="40dp"
        android:src="@drawable/pencil"
        app:backgroundTint="@color/accentColor"
        app:borderWidth="0dp"
        app:elevation="8dp"
        app:layout_anchor="@id/cv"
        app:layout_anchorGravity="end|bottom"
        app:pressedTranslationZ="12dp"
        app:rippleColor="@android:color/white" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_persistent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="40dp"
        android:src="@drawable/pencil"
        app:backgroundTint="@color/accentColor"
        app:borderWidth="0dp"
        app:elevation="8dp"
        app:layout_anchor="@id/cv2"
        app:layout_anchorGravity="end|bottom"
        app:pressedTranslationZ="12dp"
        app:rippleColor="@android:color/white" />

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

Working:

Not working:

Upvotes: 1

Views: 1480

Answers (1)

gdaramouskas
gdaramouskas

Reputation: 3747

First of all keep in mind that you have named the first CardView as cv2 and the second one as cv. Therefore the button that has the issue is the first one on your xml and not the second one.

Later on, the app:layout_behavior="@string/appbar_scrolling_view_behavior" indicates that you want your NestedScrollView child views to be automatically scrolled. Now, since your buttons' positions depend on one of the NestedScrollViews children check if these causes the issue.

Also, you might want to get the FABs to be children of your LinearLayout inside your CardView's. When the orientation changes, the activity/fragment is re-created and all the layout parameters are re-initialized and maybe something breaks there.

Also, take a look at a similar problem:

Floating Action Button not showing fully inside a fragment

Upvotes: 1

Related Questions