Search code examples
androidandroid-layoutandroid-actionbarbottom-sheet

Persistent BottomSheet below ActionBar


I have an app layout with a custom toolbar and a persistent BottomSheet - both inside of a CoordinatorLayout.

On a button click I want to show the BottomSheet. Right now the sheet is displayed fullscreen and overlays the toolbar. By setting the app theme to Theme.AppCompat.Light.DarkActionBar the BottomSheet stays below the ActionBar, but the bar cannot be customized.

Is there a way to limit the height of the persitent BottomSheet to fullscreen - ActionBar height?

This is my code in activity_main.xml

<android.support.design.widget.CoordinatorLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:attrs="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="com.test.MainActivity">

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        app:elevation="20dp"
        android:elevation="20dp"
        android:layout_height="?attr/actionBarSize">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            app:elevation="20dp"
            android:elevation="20dp"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>
    </android.support.design.widget.AppBarLayout>
    </LinearLayout>
    <include layout="@layout/bottom_sheet_additem"/>
</CoordinatorLayout>

Here is the code of sheet_bottom.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottomSheetLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    app:behavior_hideable="true"
    app:behavior_peekHeight="0dp"
    android:fitsSystemWindows="true"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <TextView
        android:id="@+id/bottomsheet_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Lorem Ipsum Dolor..."
        android:textColor="#FFFFFF" />
</RelativeLayout>

Screenshot The image on the left hand side shows the BottomSheet which stops below the Toolbar - which is not working with my current code. Currently it looks like the picture on the right.


Solution

  • I had the same problem... I don't know if it's the best solution, but for now, worked for me.

    Try to put your include inside another CoordinatorLayout in your activity_main.xml, with a marginTop like this:

    <android.support.design.widget.CoordinatorLayout
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:attrs="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="com.test.MainActivity">
    
        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
    
            <android.support.design.widget.AppBarLayout
              android:layout_width="match_parent"
              app:elevation="20dp"
              android:elevation="20dp"
              android:layout_height="?attr/actionBarSize">
    
              <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                app:elevation="20dp"
                android:elevation="20dp"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"/>
            </android.support.design.widget.AppBarLayout>
        </LinearLayout>
    
        <android.support.design.widget.CoordinatorLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_marginTop="56dp">
          
          <include layout="@layout/bottom_sheet_additem"/>
    
        </android.support.design.widget.CoordinatorLayout>
    
    </CoordinatorLayout>
    

    I hope it helps.