Material-components-android: App Theme does not get applied to BottomSheetDialogFragment

Created on 10 May 2018  路  11Comments  路  Source: material-components/material-components-android

Problem

The App Theme does not get appplied when using BottomSheetDialogFragment.

Reproduction steps

  • Like advised, I extended BottomSheetDialogFragment like so:
class LoginSheet : BottomSheetDialogFragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.login_sheet, container, false)
    }
}
  • Also, like advised, I use this code to show the Dialog:
    LoginSheet().showNow(fragmentManager, "LoginDialog")
  • The BottomSheet shows up without the desired theme.

    Version number

    • 1.0.0-alpha1

Operating system and device

  • Android 8.1 (AOSP) on OnePlus One

    Related issues

Most helpful comment

Use it like this to apply the AppTheme

 override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View? {
    val contextThemeWrapper = ContextThemeWrapper(activity, R.style.AppTheme) // your app theme here
    return inflater.cloneInContext(contextThemeWrapper).inflate(R.layout.login_sheet, container, false)
  }

All 11 comments

Use it like this to apply the AppTheme

 override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View? {
    val contextThemeWrapper = ContextThemeWrapper(activity, R.style.AppTheme) // your app theme here
    return inflater.cloneInContext(contextThemeWrapper).inflate(R.layout.login_sheet, container, false)
  }

This is happening because the default bottomSheetDialogTheme attribute points to a style that isn't a ThemeOverlay, but rather a full theme that defines the default colors and such. This should be fixed in the library IMO.

Here's a somewhat clean workaround:

<style name="Theme.YourApp" parent="Theme.MaterialComponents.*something*">
  ...
  <item name="bottomSheetDialogTheme">@style/ThemeOverlay.YourApp.BottomSheetDialog</item>
  ...
</style>

<style name="ThemeOverlay.YourApp.BottomSheetDialog" parent="ThemeOverlay.MaterialComponents.Dialog">
  <item name="android:windowBackground">@android:color/transparent</item>
  <item name="android:windowAnimationStyle">@style/Animation.MaterialComponents.BottomSheetDialog</item>
  <item name="bottomSheetStyle">@style/Widget.MaterialComponents.BottomSheet.Modal</item>
</style>

I've added a bug to track this issue: https://issuetracker.google.com/issues/120073616

I can confirm that this fixes the theming issue. @DSteve595 would you like me to submit a pull request on your behalf?

A pull request would be great, but I'm not familiar enough with the structure of the styles.xml to be confident in doing it properly.

Please comment on the bug with any other updates.

@ymarian Is this fixed? Or is the tracking being moved to https://issuetracker.google.com/issues/120073616?

@DSteve595 just the latter but it鈥檚 one of our priorities now. Hopefully syncing a fix soon. I can comment again here when that happens

Awesome, thank you so much!

Has ThemeOverlay.MaterialComponents.BottomSheetDialog been removed since this issue?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ataulm picture ataulm  路  3Comments

KelvinPac picture KelvinPac  路  3Comments

gabrielemariotti picture gabrielemariotti  路  3Comments

gabrielemariotti picture gabrielemariotti  路  3Comments

JakeWharton picture JakeWharton  路  3Comments