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>