Persistent Bottom Sheet with rounded corners in Android crashes when used in a card view
Asked Answered
A

4

6

I have created a persistent bottom sheet in android with the intent of displaying a ListView containing additional information about locations. I want the sheet to have rounded corners. I got a ton of results for modal dialog but none for persistent. Is it possible or should I use the modal version?

As suggested in an answer here, I tried wrapping it in a card view but the app crashes when I try to open the fragment.


<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".fragments.MapFragment">

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

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            app:cardCornerRadius="20dp">

            <fragment
                android:id="@+id/autocomplete_fragment"
                android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:layout="@layout/places_autocomplete_item_powered_by_google" />

        </androidx.cardview.widget.CardView>

        <com.google.android.gms.maps.MapView
            android:id="@+id/mapView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </RelativeLayout>

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="450dp"
        app:cardCornerRadius="24dp"
        app:cardElevation="8dp"
        app:layout_behavior="@string/bottom_sheet_behavior"
        app:behavior_hideable="true"
        app:behavior_peekHeight="55dp">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/tvBottomSheet"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/placeholder_text"
                android:textColor="@color/colorCommon_BLACK"
                android:textSize="37sp" />

        </androidx.core.widget.NestedScrollView>

    </androidx.cardview.widget.CardView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Alyssa answered 20/6, 2019 at 8:51 Comment(3)
check this question : #43853062Ventilator
@RasoolGhana I did. It's for modal dialog. Not working for me.Alyssa
@nachiketa I tried your code, maybe something wrong with your fragment, remove fragment and your layout works fine. I copy your layout and only remove fragment and android:textColor, android:text="@string/placeholder_text" in TextViewCentra
A
3

U can add shape for you bottom sheet background background

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <solid android:color="@color/you_color"/>

    <corners
            android:bottomRightRadius="2dp"
            android:bottomLeftRadius="2dp"
            android:topLeftRadius="2dp"
            android:topRightRadius="2dp"/>
</shape>
Antin answered 20/6, 2019 at 9:32 Comment(0)
L
13

With the new Material Component library you can customize the shape of your component using the shapeAppearanceOverlay attribute.

You can use something like:

  <!-- BottomSheet -->
  <style name="CustomBottomSheet" parent="Widget.MaterialComponents.BottomSheet">
    <item name="shapeAppearanceOverlay">@style/CustomShapeAppearanceOverlay.MaterialComponents.BottomSheet</item>
    ....
  </style>

  <style name="CustomShapeAppearanceOverlay.MaterialComponents.BottomSheet" parent="">
    <item name="cornerSizeTopRight">16dp</item>
    <item name="cornerSizeTopLeft">16dp</item>
    <item name="cornerSizeBottomRight">0dp</item>
    <item name="cornerSizeBottomLeft">0dp</item>
  </style>

Then you can apply this style to your single component or your theme app.

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    ...
    <item name="bottomSheetStyle">@style/CustomBottomSheet</item>
  </style>

If you are using a non-modal bottom sheet just apply the style. For example:

<LinearLayout
    android:id="@+id/standardBottomSheet"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    style="?attr/bottomSheetStyle" 
    ...>

enter image description here

Logo answered 23/8, 2019 at 12:54 Comment(4)
For me the app crashedPerished
@Perished It is just a style without code, quite difficult to have a crash. Post a question with all details and your stacktrace.Logo
One thing to note--if you set your drawer to STATE_EXPANDED, it wipes out the rounded corners due to this ridiculous google bug.Instructive
@ChristopherPickslay check also #43853062Logo
A
3

U can add shape for you bottom sheet background background

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <solid android:color="@color/you_color"/>

    <corners
            android:bottomRightRadius="2dp"
            android:bottomLeftRadius="2dp"
            android:topLeftRadius="2dp"
            android:topRightRadius="2dp"/>
</shape>
Antin answered 20/6, 2019 at 9:32 Comment(0)
L
1

You could also use the app:shapeAppearance attribute:

Create a ShapeAppearance in styles.xml:

<resources>

    ...

    <style name="BottomSheetShapeAppearance">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSizeTopLeft">16dp</item>
        <item name="cornerSizeTopRight">16dp</item>
    </style>

</resources>

Set the app:shapeAppearance attribute on the BottomSheet component:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/Widget.MaterialComponents.BottomSheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    app:shapeAppearance="@style/BottomSheetShapeAppearance">

    ...

</LinearLayout>

Here's how it looks:

Screenshot

More info: https://material.io/develop/android/theming/shape

Linnlinnaeus answered 12/9, 2022 at 14:13 Comment(0)
C
0

You can wrap your sheet layout with a card view to have rounded corners.

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

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="450dp"
        app:layout_behavior="@string/bottom_sheet_behavior"
        app:cardCornerRadius="24dp"
        app:cardElevation="8dp"
        app:behavior_hideable="true"
        app:behavior_peekHeight="55dp">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/tvBottomSheet"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="ABC"
                android:textSize="37sp" />

        </androidx.core.widget.NestedScrollView>

    </androidx.cardview.widget.CardView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Centra answered 20/6, 2019 at 9:2 Comment(4)
@nachiketa Sorry, try again with androidx.cardview.widget.CardView as my edited answer.Centra
The app still crashes without anything on the logcat.Alyssa
Is there anything wrong? I still running without any issues. Check my last edited, if it still doesn't work, just wrap your NestedScrollView with androidx.cardview.widget.CardView and set these attributes: app:layout_behavior, app:behavior_hideable, app:behavior_peekHeight, app:cardCornerRadius, app:cardElevationCentra
@nachiketa Show all your layout file to inspect more detailCentra

© 2022 - 2024 — McMap. All rights reserved.