Bottomsheetdialog with Transparent Background

BottomSheetDialog with transparent background

So, I figured out 2 solutions.

Best one:

Create an activity with transparent background just for your bottom sheet.
Implement your own layout with a coordinator layout and a bottom sheet.
Set the margin you want.
Set the content you want.

Not tested yet.

Lazy one:

Extends BottomSheetDialogFragment, in onActivityCreated add:

    Resources resources = getResources();

// Set margin for Landscape Mode. Maybe a more elegant solution will be to implements our own bottom sheet with our own margins.
if (resources.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
assert getView() != null;
View parent = (View) getView().getParent();
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
layoutParams.setMargins(
resources.getDimensionPixelSize(R.dimen.bottom_sheet_margin_left), // 64dp
0,
resources.getDimensionPixelSize(R.dimen.bottom_sheet_margin_right), // 64dp
0
);
parent.setLayoutParams(layoutParams);
}

Android bottomsheet dialog tansparent background

Maybe you should also set your Dialog Window to transparent.

add the following code to your BottomSheetDialog.

// BottomSheetDialog
public BottomSheetDialog(Context context) {
super(context, R.style.Bottom_Sheet_Style); //set your own dialog theme
// ...
Window window = getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
WindowManager.LayoutParams lp = window.getAttributes();
lp.alpha = 1.0f;
lp.dimAmount = 0.0f;
window.setAttributes(lp);
// ...
}

Then add the following code to your res/values/styles.xml. If you don't have the file, then create one.

<style name="Bottom_Sheet_Style" parent="@android:style/Theme.Dialog">  
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
</style>

Remove White Background in Bottom Sheet Dialog Fragment

You will need to set transparent background to bottom sheet view itself.

Here is an example is Kotlin:

class YourBottomSheetFragment : BaseBottomSheetDialogFragment() {

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.setOnShowListener { setupBottomSheet(it) }
return dialog
}

private fun setupBottomSheet(dialogInterface: DialogInterface) {
val bottomSheetDialog = dialogInterface as BottomSheetDialog
val bottomSheet = bottomSheetDialog.findViewById<View>(
com.google.android.material.R.id.design_bottom_sheet)
?: return
bottomSheet.setBackgroundColor(Color.TRANSPARENT)
}
}


Related Topics



Leave a reply



Submit