0

I have "activity_main" in which I'm using a Collapsing Toolbar and a Fragment Container. Inside container I'm adding and replacing multiple fragments one of them is "activity_user_shops_list" which is a recycler view. I want to align fab to the bottom right of the screen inside this list fragment. I can't find a way to align it. Any help is appreciated.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="#F3F3F3">


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/id_appbar_layout_m"
        android:layout_width="match_parent"
        android:layout_height="200dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/id_collapsing_toolbar_m"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
            app:titleCollapseMode="scale"
            app:title="@string/user_name"
            app:expandedTitleGravity="center_horizontal"
            app:expandedTitleMarginTop="160dp"
            android:background="#F3F3F3">


            <com.google.android.material.imageview.ShapeableImageView
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:src="@mipmap/ic_launcher"
                app:shapeAppearanceOverlay="@style/circleImageView"
                android:layout_gravity="center"/>


            <com.google.android.material.appbar.MaterialToolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"/>


        </com.google.android.material.appbar.CollapsingToolbarLayout>


    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/id_appbar_layout_m"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/id_fragment_container_m"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"/>

    </androidx.core.widget.NestedScrollView>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

fragment_user_shops_list.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/null_gray">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/id_recycler_view_e_shops"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/id_floating_button_e_shops"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/add_a_shop"
        android:src="@drawable/ic_plus"
        app:tint="@color/white"
        app:maxImageSize="30dp"
        app:useCompatPadding="true"/>

</RelativeLayout>

Edit:

After adding android:layout_alignParentRight="true" and
android:layout_alignParentBottom="true" and making all fab parents match_parent, It still not working.

No mater what I do result is same. Here is the result (Gray area is "fragment_user_shops_list"):

Screenshot:

Intelligence
  • 33
  • 1
  • 6

1 Answers1

0

You need to set android:layout_alignParentRight and android:layout_alignParentBottom to your FloatingActionButton to align to bottom right corner

Here's what updated xml file should look like:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/id_recycler_view_e_shops"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/id_floating_button_e_shops"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="16dp"
        android:contentDescription="Shop"
        android:src="@android:drawable/btn_plus"
        app:maxImageSize="30dp"
        app:tint="@android:color/white"
        app:useCompatPadding="true" />

</RelativeLayout>
Nitish
  • 3,075
  • 3
  • 13
  • 28
  • You need to set 'FragmentContainerView' height to match parent also to take up the full space available – Nitish Sep 16 '21 at 12:32
  • Yes as I mentioned in my edit I changed all parents of fab including nested scroll view and fragment container width and height to match_parent and it still does not work. – Intelligence Sep 16 '21 at 13:56