material-components / material-components/material-components-android

[MaterialDatePicker] Cannot use as normal fragment.

Open
#3,426 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Widget: DatePicker
Dominant language
Java
Stars
17.4k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

**Description:** Cannot use MaterialDatePicker as normal fragment to add to a fragment container.

**Expected behavior:** A DialogFragment should be used as a normal Fragment.

**Source code:**

When using dynamic color theme and them add MaterialDatePicker as a normal fragment via:
```
val picker = MaterialDatePicker.Builder.datePicker().build()
picker.showsDialog = false
supportFragmentManager.beginTransaction().add(R.id.container, picker).commit()
```
then we got crashed:
```
com.example.materialdatepickerissue E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.materialdatepickerissue, PID: 3568
android.view.InflateException: Binary XML file line #23 in com.example.materialdatepickerissue:layout/mtrl_picker_dialog: Binary XML file line #31: You must supply a layout_width attribute., theme={InheritanceMap=[id=0x7f100268com.example.materialdatepickerissue:style/Theme.MaterialDatePickerIssue, id=0x7f10022fcom.example.materialdatepickerissue:style/Theme.Material3.DynamicColors.DayNight, id=0x7f100230com.example.materialdatepickerissue:style/Theme.Material3.DynamicColors.Light, id=0x7f100231com.example.materialdatepickerissue:style/Theme.Material3.Light, id=0x7f10005bcom.example.materialdatepickerissue:style/Base.Theme.Material3.Light, id=0x7f1000adcom.example.materialdatepickerissue:style/Base.V24.Theme.Material3.Light, id=0x7f100087com.example.materialdatepickerissue:style/Base.V14.Theme.Material3.Light, id=0x7f100256com.example.materialdatepickerissue:style/Theme.MaterialComponents.Light, id=0x7f100068com.example.materialdatepickerissue:style/Base.Theme.MaterialComponents.Light, id=0x7f1000a1com.example.materialdatepickerissue:style/Base.V21.Theme.MaterialComponents.Light, id=0x7f10008fcom.example.materialdatepickerissue:style/Base.V14.Theme.MaterialComponents.Light, id=0x7f100090com.example.materialdatepickerissue:style/Base.V14.Theme.MaterialComponents.Light.Bridge, id=0x7f100134com.example.materialdatepickerissue:style/Platform.MaterialComponents.Light, id=0x7f100210com.example.materialdatepickerissue:style/Theme.AppCompat.Light, id=0x7f100050com.example.materialdatepickerissue:style/Base.Theme.AppCompat.Light, id=0x7f1000b3com.example.materialdatepickerissue:style/Base.V28.Theme.AppCompat.Light, id=0x7f1000b0com.example.materialdatepickerissue:style/Base.V26.Theme.AppCompat.Light, id=0x7f1000aacom.example.materialdatepickerissue:style/Base.V23.Theme.AppCompat.Light, id=0x7f1000a8com.example.materialdatepickerissue:style/Base.V22.Theme.AppCompat.Light, id=0x7f10009dcom.example.materialdatepickerissue:style/Base.V21.Theme.AppCompat.Light, id=0x7f1000b6com.example.materialdatepickerissue:style/Base.V7.Theme.AppCompat.Light, id=0x7f100131com.example.materialdatepickerissue:style/Platform.AppCompat.Light, id=0x7f10013ccom.example.materialdatepickerissue:style/Platform.V25.AppCompat.Light, id=0x1030241android:style/Theme.Material.Light.NoActionBar, id=0x1030237android:style/Theme.Material.Light, id=0x103000candroid:style/Theme.Light, id=0x1030005android:style/Theme],
```
It say that `layout/mtrl_picker_dialog` is missing `layout_width` attribute, found this is missing in `mtrl_picker_header_dialog.xml` that controled by `?attr/materialCalendarHeaderLayout`, which is :
```

<item name="android:background">@android:color/transparent</item>


<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">@dimen/mtrl_calendar_header_height</item>
<item name="android:background">?attr/colorPrimary</item>

```
It's odd.

**Minimal sample app repro:**

[MaterialDatePickerIssue](https://github.com/kolyneh/MaterialDatePickerIssue)

**Android API version:** Android API 33

**Material Library version:** 1.9.0

**Device:** AVD API 33

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the API 33 crash using the linked MaterialDatePickerIssue sample and the normal-fragment setup described in the issue. Inspect layout/mtrl_picker_dialog and mtrl_picker_header_dialog.xml, especially the materialCalendarHeaderLayout style; done means MaterialDatePicker can be added to a fragment container without the InflateException.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.