Material date picker not showing

31 views Asked by At

I am using material date picker in my andorid project in fragment. On specific click, I want to show user the date dialog but on click nothing is showing.

Here is my code:

Code screenshot

I am expecting that user select two date range and I get those date

2

There are 2 answers

1
Tommly Jumah On BEST ANSWER

There could be several reasons why this might be happening. Here are some things to check:

Button Click Listener: Ensure that you have properly set up the click listener for the button that triggers the date picker dialog. Make sure you're assigning the calendarView() function to the button's setOnClickListener() method.

here is an example // Inside your fragment class

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {  super.onViewCreated(view, savedInstanceState)
       val button = view.findViewById<Button>(R.id.button_date_picker)    button.setOnClickListener {
       calendarView()    } }
       private fun calendarView() {    val materialDatePicker = MaterialDatePicker.Builder.dateRangePicker()
       .setTitleText("Select date")
       .setSelection(
           androidx.core.util.Pair(
               MaterialDatePicker.thisMonthInUtcMilliseconds(),
               MaterialDatePicker.todayInUtcMilliseconds()
           )
       )
       .setPositiveButtonText("Submit")
       .build()
       materialDatePicker.addOnPositiveButtonClickListener { pair ->
       val simpleDateFormat = SimpleDateFormat("EEEE, dd MMMM yyyy", Locale.getDefault())
       val startingDate = Date(pair.first)
       val startDate = simpleDateFormat.format(startingDate)
       val endingDate = Date(pair.second)
       val endDate = simpleDateFormat.format(endingDate)
   
       Toast.makeText(requireContext(), "Submitted", Toast.LENGTH_SHORT).show()
       Log.d("date---->>", "${startDate.toString()} ${endDate.toString()}")
       // dateTextView.text = "$f

ormattedStartDate until $formattedEndDate" } materialDatePicker.show(requireFragmentManager(), materialDatePicker.toString()) }

Make sure your fragment layout XML file contains a button with the ID button_date_picker. If all these steps are correct and the issue still persists, you might want to provide more details.

0
Iftekhar On

you forgot show function

materialDatePicker.show(supportFragmentManager, "DATE_PICKER_RANGE")