Search code examples
androidandroid-layoutfloating-action-buttonmaster-detail

Android Master/Detail - have FAB anchored to master on single or multi pane


I would like a Floating Action Button (FAB) to be anchored to the master pane when showing two panes on a tablet like the Gmail app does (see attached image) but have been unable to code this functionality using the Master/Detail template in Android Studio.

The default behavior is to have the FAB display on the bottom/right of the screen in both single or multi-pane mode.

I've tried moving the FAB from the activity_item_list.xml to the layouts containing the RecyclerView but that causes the detail pane to not be displayed in multi-pane mode.

Is it possible to have the FAB also display anchored to the master using the layouts from the master-detail template or is it necessary to start from scratch and perhaps use multiple fragments and activities?

Showing FAB moving to be anchored to the list pane

Below is the altered layout for the item_list now containing the FAB. I added a RelativeLayout and the FAB over what was originally in the template.

<LinearLayout 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:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:baselineAligned="false"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    android:showDividers="middle"
    tools:context=".ItemListActivity">

    <!-- This layout is a two-pane layout for the Items master/detail flow.-->

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

        <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/item_list"
            android:name=".ItemListFragment"
            android:layout_width="@dimen/item_width"
            android:layout_height="match_parent"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:layoutManager="LinearLayoutManager"
            tools:context=".ItemListActivity"
            tools:listitem="@layout/item_list_content" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:src="@android:drawable/ic_dialog_email" />
    </RelativeLayout>

    <FrameLayout
        android:id="@+id/item_detail_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />

</LinearLayout>

Solution

  • The issue was the layout. Adding a 2nd relative layout and placing the detail to the right of the master solved the issue.

    <LinearLayout 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:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:baselineAligned="false"
        android:divider="?android:attr/dividerHorizontal"
        android:orientation="horizontal"
        android:showDividers="middle"
        tools:context=".ItemListActivity">
    
        <!--This layout is a two-pane layout for the Items master/detail flow.-->
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/item_list_master">
    
            <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/item_list"
                android:name=".ItemListFragment"
                android:layout_width="@dimen/item_width"
                android:layout_height="match_parent"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:layoutManager="LinearLayoutManager"
                tools:context=".ItemListActivity"
                tools:listitem="@layout/item_list_content" />
    
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_margin="@dimen/fab_margin"
                android:layout_alignRight="@id/item_list"
                android:src="@android:drawable/ic_dialog_email" />
    
        </RelativeLayout>
            <FrameLayout
                android:id="@+id/item_detail_container"
                android:layout_width="400dp"
                android:layout_height="match_parent"
                android:layout_toRightOf="@+id/item_list_master"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />
    
        </RelativeLayout>
    </LinearLayout>