Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottom Sheet Above Bottom Navigation

My goal is to place a "bottom sheet" on top of a BottomNavigationView like this:

enter image description here

But it stays the following way. Both views collapse:

enter image description here

This is the xml of my main activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
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:background="@drawable/tierrota"
tools:context="com.example.juanjose.myapplication.ViajesActivity">

<!-- include main content -->
<include layout="@layout/bottomsheet" />

<!-- include bottom sheet -->
<include layout="@layout/bottom_navigation" />

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

Code of bottom_navigation:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:itemBackground="@color/colorClarito"
    app:itemIconTint="@drawable/nav_item_color_state"
    app:itemTextColor="@drawable/nav_item_color_state"
    app:menu="@menu/bottom_navigation_main" />

</RelativeLayout>

And code of bottom sheet

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="80dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

<TextView
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="@color/colorBackgroundSemi"
    android:gravity="center"
    android:text="Bandeja de entrada"
    android:fontFamily="@font/eraslght"
    android:textColor="@color/colorLetra"
    app:layout_anchor="@+id/bottom_navigation"
    app:layout_anchorGravity="top"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="coisa2"
    android:textColor="@android:color/white" />

</LinearLayout>

I am new with these two elements. Is there someone who knows any way to achieve what I'm looking for?

I want my "bottom sheet" to act as such and can expand. My ultimate goal is to add a RecyclerView inside the BottomSheet.

like image 748
doblejota93 Avatar asked Feb 06 '18 18:02

doblejota93


2 Answers

To use the BottomSheet, it should be a child of the CoordinatorLayout. Here I have used relative layout as a parent layout for bottom navigation to stay at the bottom and then CoordinatorLayout above bottom navigation. Here's a article that will help you.

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_above="@id/bottom_navigation_parent"
        android:layout_height="match_parent">


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


            <FrameLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="?attr/colorPrimary" />


        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/bottom_sheet_parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#000000"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

            <LinearLayout
                android:id="@+id/bottom_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <View
                    android:layout_width="20dp"
                    android:layout_height="4dp"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/view_bottom_sheet_top" />

                <TextView
                    android:id="@+id/near_by"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/normal"
                    android:gravity="center"
                    android:includeFontPadding="false"
                    android:padding="10dp"
                    android:text="Book Now"
                    android:textAllCaps="true"
                    android:textColor="?attr/colorPrimaryText"
                    android:textSize="12sp" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view_maps"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/bottom_view"
                android:layoutAnimation="@anim/layout_animation">

            </android.support.v7.widget.RecyclerView>

        </RelativeLayout>



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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/bottom_navigation_parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:fitsSystemWindows="true"
        app:elevation="1dp">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorContainerBackground"

            />
<!--              Start BottomNavigationView -->

       
<!--              End BottomNavigationView -->


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


</RelativeLayout>

Don't forget to add this in your Activity/Fragment

private RelativeLayout bottomSheetParentLayout;
    private BottomSheetBehavior mBottomSheetBehaviour;

   mBottomSheetBehaviour = BottomSheetBehavior.from(bottomSheetParentLayout);

  if (bottomNavigation != null) {

        mBottomSheetBehaviour.setPeekHeight(bottomNavigation.getHeight() + 90);

    }

Output will look like this

like image 103
iamnaran Avatar answered Oct 12 '22 23:10

iamnaran


Wrap the two elements inside a linear layout. Not sure how coordinator layout behaves but I think it will not allow you to "order" elements (similar to frame layout).

like image 30
Hendrik Marx Avatar answered Oct 13 '22 01:10

Hendrik Marx