How to remove white space from BottomSheetDialog when adding a fake shadow?

1.1k views Asked by At

I'm using this SO answer to simulate elevation on my BottomSheetDialogFragment because elevation doesn't work with it.

https://stackoverflow.com/a/40927770/11110509

However when I add that fake shadow, there is unwanted white space unlike that SO answer. How do I remove it?

enter image description here

<?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="300dp"
    android:elevation="6dp">

    <View
        android:id="@+id/fragplaces_top_shadow"
        android:layout_width="match_parent"
        android:layout_height="16dp"
        android:background="@drawable/shadow_dialog_places_top"/>

    <TextView
        android:id="@+id/fragplaces_app_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Places near me"
        android:textColor="@color/colorWhite"
        android:textStyle="bold"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/fragplaces_top_shadow"
        android:paddingStart="12dp"
        android:paddingEnd="12dp"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"
        android:background="@color/standardBlue"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/fragadd_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/fragplaces_app_name"
        android:background="@color/colorWhite"/>

</RelativeLayout>

Heres the faux shadow:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="90"
        android:endColor="@android:color/transparent"
        android:startColor="#64000000"/>
</shape>

Adding a transparent background to the parent layout doesn't work either:

android:background="@android:color/transparent"

How do I remove the unwanted white space in my fake shadow?

2

There are 2 answers

0
private static On

Inside your dialog set up, try setting the background to transparent:

View view = View.inflate(getContext(), R.layout.fragment_bottom_sheet, null);
dialog.setContentView(view);

( (View) view.getParent()).setBackgroundColor(Color.TRANSPARENT);

Notice that you apply it to the view not the dialog.

0
Arnold Brown On

Simple hack

You can give android:layout_marginTop="-5dp"(or -3dp to -5dp) to View (fragplaces_top_shadow)