2

I want to make the layout of a BottomSheet with rounded corners, but setting a drawable with corner radius does not clip the layout background. I am using a BottomSheetDialogFragment.

fragment_a.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/round_rectangle">
   <!-- other views here -->
</androidx.constraintlayout.widget.ConstraintLayout>

round_rectangle.xml

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"   >
    <solid
        android:color="@android:color/white" />
    <stroke
        android:width="2dp"
        android:color="#C4CDE0" />
    <padding
        android:left="0dp"
        android:right="0dp"
        android:top="0dp"
        android:bottom="0dp" />
    <corners
        android:topLeftRadius="16dp"
        android:topRightRadius="16dp" />
</shape>

Current result:

uncliped layout

Tried:

Clipping programetically using

view.clipToOutline = true

Please help! Thanks in advance!

Community
  • 1
  • 1
user158
  • 12,852
  • 7
  • 62
  • 94

1 Answers1

2

The color of your rounded corners is coming from the color of the bottom sheet container. To determine how to make our transparent corners, we need to inspect the layout. The layout inspector identifies the key components that interest us: the bottom sheet itself (id/bottomSheet) and its frame (id/design_bottom_sheet).

enter image description here

We will need to change the background color of the bottom sheet frame, id/design_bottom_sheet, to transparent to get our rounded corners.

Finding the frame is easy once it is available. One place to set the frame's background, once the dialog is created and the fragment's creation is far enough along, is in onActivityCreated() of your custom BottomSheetDialogFragment. At this point in the fragment's lifecycle, the view hierarchy is instantiated.

@Override
public void onActivityCreated(Bundle bundle) {
    super.onActivityCreated(bundle);
    View frameParent = ((View) getDialog().findViewById(R.id.bottomSheet).getParent());
    frameParent.setBackgroundColor(Color.TRANSPARENT);
}

enter image description here

You could also just do a findViewById() for the frame itself:

getDialog().findViewById(R.id.design_bottom_sheet).setBackgroundColor(Color.TRANSPARENT)

Either way depends on a knowledge of internal structure of the BottomSheetDialogFragment, so pick the one that you prefer.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131