androidandroid-datepickermaterial-components-androidmaterial-componentsdatepickerdialog

How to set texts on MaterialDatePicker?


To change the title we use this code

builder.setTitleText()


But how to set selected date range texts: "Start date" and "End date".
For example, "Start date" to "Text1" and "End date" to "Text2".

enter image description here


Thank you in advance!!!


Solution

  • You can use the builder.setSelection method:

        val builder = MaterialDatePicker.Builder.dateRangePicker()
        val selection = Pair(startDate,endDate)
        builder.setSelection(selection)
    

    where startDate and endDate are long value

    enter image description here


    About the placeholders.

    Currently there isn't a method to change the placeholders for two dates when they are unselected.

    However you can override all the existing strings in your project, but this workaround can stop to run in the next releases.

    Add in your strings.xml

    <string name="mtrl_picker_range_header_unselected"
            description="Placeholders for two dates separated by a dash representing a range [CHAR_LIMIT=60]">Start date – End date</string>
    

    enter image description here

    Here the other strings when the dates are selected:

      <string name="mtrl_picker_range_header_only_start_selected" description="Placeholders for two dates separated by a dash representing a range where start date has been selected [CHAR_LIMIT=60]">%1$s – End date</string>
      <string name="mtrl_picker_range_header_only_end_selected" description="Placeholders for two dates separated by a dash representing a range where end date has been selected [CHAR_LIMIT=60]">Start date – %1$s</string>
      <string name="mtrl_picker_range_header_selected" description="Two dates separated by a dash representing a range [CHAR_LIMIT=60]">%1$s – %2$s</string>