4

I´m attempting to use the new Android Design Library´s CoordinatorLayout inside of another layout. The CoordinatorLayout contains a RecyclerView. What I´m attempting to do is to place a simple LinearLayout below the CoordinatorLayout. This layout should be placed below the CoordinatorLayout. This is XML I´m using:

<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:fitsSystemWindows="true"
    android:orientation="vertical">

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

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary" />

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/conversation_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal">

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="match_parent" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/ic_send_white_36dp" />

    </LinearLayout>
</LinearLayout>

As you can see, I´m attempting to wrap the CoordinatorLayout inside another LinearLayout. However, on-screen the second part of the layout doesn´t even render.

Lamorak
  • 10,190
  • 8
  • 44
  • 55
stamppot
  • 195
  • 1
  • 2
  • 10

1 Answers1

12

That is because the CoordinatorLayout has layout_height="match_parent". That means it takes the whole size of the screen and your LinearLayout is rendered but it is below the CoordinatorLayout and off-screen.

An easy way to fix this would be setting the weight of the CoordinatorLayout to fill the parent layout but leave space necesseary to display the footer LinearLayout. This would be

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

But this is not ideal way so I would suggest leaving the CoordinatorLayout as the root element and place your LinearLayout just bellow the RecyclerView where it belongs. Since your footer LinearLayout has fixed height this can be done easily

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <android.support.design.widget.AppBarLayout>
        <android.support.v7.widget.Toolbar />
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:paddingBottom="150dp" />

    <LinearLayout
        android:layout_height="150dp"
        android:layout_gravity="bottom">
        <EditText />
        <ImageButton />
    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>
Lamorak
  • 10,190
  • 8
  • 44
  • 55
  • This is indeed what I ended up doing as well. I put in a fixed padding at the bottom. However, I´m weary as to how this will look on lower-resolution screens. – stamppot Jun 29 '15 at 11:38
  • The `dp` unit is well designed and will mostly work OK. In case it didn't you can always specify different [dimensions](http://developer.android.com/guide/topics/resources/more-resources.html#Dimension) for different screen sizes. – Lamorak Jun 29 '15 at 11:41
  • I am using this code with the exception that instead of `RecyclerView` i have a `FrameLayout` which is replace with `Fragments` at runtime by the activity. The fragments have NestedScrollView, RecyclerView etc in them. But in my case the `LinearLayout` (`Bottom Footer`) sits right below the appbar and not the end of the screen. I have tried `layout_gravity & `layout_anchor` but it sticks to the bottom of the app_bar – Sakibul Alam Jul 26 '15 at 20:32
  • Do you use weight or fixed height? – Lamorak Jul 27 '15 at 09:50