0

I have a question. I would like to disable the edge-to-edge padding in BottomSheetDialogFragment().

I’ve currently done it like this. I should mention that we have to handle onConfigurationChange for landscape ourselves, and I always run into the issue that in landscape the system automatically sets a right padding for edge-to-edge.

I’ve now done the following in our BaseBottomSheetDialogFragment, which extends BottomSheetDialogFragment: What do y think.

`/** * The system automatically adds padding to the top level of the XML in landscape mode for Android 16 / API 36+ * Example: You have a LinearLayout (Top level) inside another LinearLayout (Second level), * then the first LinearLayout sets its paddings, plus provides for edge-to-edge reservation from the system. * To avoid this behavior, the top-level layout should have its padding set to 0 programmatically. * Instead, padding should be applied to the second-level layout, since its values are not modified by the system. */ fun updatePaddingForInsets(rootView: View) { rootView.post { rootView.setPadding(0, 0, 0, 0) } }` 

1 Answer 1

1

In landscape mode on API 36+, the system adds padding to the top-level layout of a BottomSheetDialogFragment to account for edge-to-edge display (e.g., status/navigation bars). This interferes with custom padding and layout designs, especially during configuration changes like orientation switches.

Solution

Extend BottomSheetDialogFragment in a base class to disable edge-to-edge padding and manage padding in a nested view. Handle window insets and configuration changes to ensure consistent behavior. Below is the implementation:

import android.content.res.Configuration import android.os.Build import android.os.Bundle import android.view.View import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import com.google.android.material.bottomsheet.BottomSheetDialogFragment open class BaseBottomSheetDialogFragment : BottomSheetDialogFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // Disable edge-to-edge for dialog window on API 36+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { dialog?.window?.setDecorFitsSystemWindows(true) } updatePaddingForInsets(view) } /** * Disable system-added edge-to-edge padding by setting top-level view padding to 0. * Apply desired padding to a nested view. Handle insets for system bars. */ protected fun updatePaddingForInsets(rootView: View) { rootView.post { // Clear top-level padding to override system behavior rootView.setPadding(0, 0, 0, 0) // Apply padding to nested view (replace with your nested view ID) val nestedView = rootView.findViewById<View>(R.id.nested_layout) nestedView?.setPadding( resources.getDimensionPixelSize(R.dimen.your_padding_start), // e.g., 16dp resources.getDimensionPixelSize(R.dimen.your_padding_top), resources.getDimensionPixelSize(R.dimen.your_padding_end), resources.getDimensionPixelSize(R.dimen.your_padding_bottom) ) } // Handle system bar insets ViewCompat.setOnApplyWindowInsetsListener(rootView) { v, insets -> val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) val nestedView = v.findViewById<View>(R.id.nested_layout) nestedView?.setPadding( systemBars.left + resources.getDimensionPixelSize(R.dimen.your_padding_start), systemBars.top + resources.getDimensionPixelSize(R.dimen.your_padding_top), systemBars.right + resources.getDimensionPixelSize(R.dimen.your_padding_end), systemBars.bottom + resources.getDimensionPixelSize(R.dimen.your_padding_bottom) ) WindowInsetsCompat.CONSUMED } } override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) // Reapply padding on orientation change view?.let { updatePaddingForInsets(it) } } } 

Example XML Layout

Ensure your layout has a nested view to apply padding:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:id="@+id/nested_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- Your content here --> </LinearLayout> </LinearLayout> 

Define padding in res/values/dimens.xml:

<resources> <dimen name="your_padding_start">16dp</dimen> <dimen name="your_padding_top">16dp</dimen> <dimen name="your_padding_end">16dp</dimen> <dimen name="your_padding_bottom">16dp</dimen> </resources> 

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.