JB2
JB2

Reputation: 1607

Transparent bottom sheet layout in Android

I am trying to make a transparent bottom sheet layout, that would allow me to see the contents of the view below it. The bottom sheet is working as expected, however when I set the background to either @null or @android:color/transparent, the layout's view is white, as opposed to transparent. My layout is as follows:

app_bar_main.xml:

<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:id="@+id/coordinatorLayout"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true"
    tools:context=".core.activities.MainActivity">
    <!-- stuff here -->
    <LinearLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@null"
        android:orientation="vertical"
        app:layout_behavior="@string/bottom_sheet_behavior">
    </LinearLayout>

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

The linear layout with id bottom_sheet holds, well, my bottom sheet. The sheet itself is defined as follows:

<?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"
    android:layout_width="match_parent"
    android:background="@null"
    android:layout_height="match_parent">
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@null"
        android:orientation="vertical">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/bottom_sheet_placeholder_layout"
            android:layout_weight="0.6"
            android:layout_width="match_parent"
            android:background="@null"
            android:layout_height="50dp"
            android:orientation="horizontal">
        </LinearLayout>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/bottom_sheet_layout"
            android:layout_margin="0dp"
            android:layout_weight="0.4"
            android:layout_width="match_parent"
            android:background="@color/my_background"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ProgressBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:id="@+id/my_progress_bar" />

            <TextView
                android:layout_marginTop="5dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:textColor="@color/my_text"
                android:id="@+id/txt_my_info"
                android:layout_gravity="center_horizontal"
                android:visibility="gone"
                android:textSize="48px" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Medium Text"
                android:id="@+id/txt_my_address"
                android:textColor="@color/my_secondary_text"
                android:visibility="gone"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>
    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/btn_edit_tree_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:layout_marginTop="-62dp"
        android:elevation="100dp"
        android:src="@drawable/ic_create_black_24dp"
        app:layout_anchor="@id/bottom_sheet_layout"
        app:layout_anchorGravity="top|end|right"
        app:useCompatPadding="true"/>
</android.support.design.widget.CoordinatorLayout>

Upvotes: 9

Views: 20917

Answers (9)

Manohar
Manohar

Reputation: 23404

Here is the kotlin version thanks to kolorszczak . Hope it save someones time converting .

val dialog = BottomSheetDialog(activity!!)
 dialog.setOnShowListener {
            var dialogTmp: BottomSheetDialog = it as BottomSheetDialog
            var bottomSheet: FrameLayout? =
                dialogTmp.findViewById(R.id.design_bottom_sheet) as FrameLayout?
                    ?: return@setOnShowListener
            bottomSheet?.background = null
        }

Upvotes: 1

TinaTT2
TinaTT2

Reputation: 489

Just need to use this style:

<style name="BaseBottomSheetDialogThem" parent="@style/Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

and set your style to BaseBottomSheetDialogThem.

Upvotes: 1

Tushar Saha
Tushar Saha

Reputation: 2106

add this in styles.xml

 <style name="TransparentDialog" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowCloseOnTouchOutside">false</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:colorBackground">     @android:color/transparent</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.3</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
</style>


   BottomSheetDialog bsDialog = new BottomSheetDialog(this,R.style.TransparentDialog);
   bsDialog.setContentView(R.layout.bottomsheet_dialog);
   bsDialog.show();

and voila it works

Upvotes: 8

user650881
user650881

Reputation: 2505

I had to wait until after setContentView was called in onActivityCreated in order to access the container. Also while getDialog().getWindow().findViewById(R.id.design_bottom_sheet) did successfully find the FrameLayout, I decided to avoid explicitly calling out the internally defined id by using getView().getParent().

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);  // setContentView called here
    ((View) getView().getParent()).setBackgroundColor(Color.TRANSPARENT);
}

Upvotes: 2

Eugene Voronoy
Eugene Voronoy

Reputation: 1433

The answer is much more easier: just add this line

myDialog .getWindow() .findViewById(R.id.design_bottom_sheet) .setBackgroundResource(android.R.color.transparent);

And don't change standard ids (R.id.design_bottom_sheet and android.R.color.transparent). It makes background of Bottom Sheet Dialog transparent

Upvotes: 12

kolorszczak
kolorszczak

Reputation: 101

private void setupDialogBackground() {
    getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            BottomSheetDialog d = (BottomSheetDialog) dialog;
            FrameLayout bottomSheet = (FrameLayout) d.findViewById(R.id.design_bottom_sheet);
            if (bottomSheet == null)
                return;
            bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
            bottomSheet.setBackground(null);
        }
    });
}

Upvotes: 9

Qamar
Qamar

Reputation: 5137

Add this in your bottomSheet setupDialog(final Dialog dialog, int style)

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

and also add android:background="@android:color/transparent" to root view

Upvotes: 1

Nguyễn Trung Hiếu
Nguyễn Trung Hiếu

Reputation: 2032

change color background

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/bottom_sheet_placeholder_layout"
        android:layout_weight="0.6"
        android:layout_width="match_parent"
        android:background="#00FFFFFF"
        android:layout_height="50dp"
        android:orientation="horizontal">
    </LinearLayout>

Try it!!

Upvotes: -1

BOUTERBIAT Oualid
BOUTERBIAT Oualid

Reputation: 1544

Create in your color.xml : <color name="colorTransparent">#00000000</color> and use android:background="@color/colorTransparent"

Upvotes: 0

Related Questions