Home > Mobile >  How can detect click cancel of DatePickerDialog?
How can detect click cancel of DatePickerDialog?

Time:12-10

I have one DatePickerDialog that show after click editText. That works well. I need to add this code that detect when user click cancel button after show Dialog then I will set editText border according this click. I generally foud java code in here. I can't use them for mine. How I detect it with my Kotlin code.

class ManuelBPEnterFragment : BaseFragment<FragmentBpInfoManuelEnterBinding>(
        FragmentBpInfoManuelEnterBinding::inflate
    ) {    
    
    private fun showDatePickerDialog() {
            DatePickerDialog(
                requireContext(),
                R.style.DatePickerTheme,
                datePicker,
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH),
            ).apply {
                datePicker.minDate = System.currentTimeMillis() - 1000
            }.show()
        }
        
        private val datePicker = DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
            calendar.set(year, month, dayOfMonth)
            binding.edtDate.setText(formatter.format(calendar.timeInMillis))
        }
    }

I tried onClick(DialogInterface.BUTTON_NEGATIVE, which: Int) inside when show Dialog. I can't write this code block truthly:

   DatePickerDialog(
        requireContext(),
        R.style.DatePickerTheme,
        datePicker,
        calendar.get(Calendar.YEAR),
        calendar.get(Calendar.MONTH),
        calendar.get(Calendar.DAY_OF_MONTH),
    ).onClick(DialogInterface.BUTTON_NEGATIVE, which: Int).apply {
        datePicker.minDate = System.currentTimeMillis() - 1000
    }.show()

CodePudding user response:

DatePickerDialog it's the same as AlertDialog, and it has this method:

public void setButton(int whichButton, CharSequence text, OnClickListener listener)

In pet project I have similar dialog creation and setting custom listener, but for BUTTON_NEUTRAL:

return DatePickerDialog(requireContext(), this,
    calendar.get(Calendar.YEAR),
    calendar.get(Calendar.MONTH),
    calendar.get(Calendar.DAY_OF_MONTH)
).apply {
    datePicker.minDate = defaultTime
    setButton(DialogInterface.BUTTON_NEUTRAL, getString(R.string.dialog_button_reset), neutralListener)
}

For BUTTON_NEGATIVE do something like that:

.apply {
    setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.dialog_button_cancel)) { dialog, i ->
        TODO("Your code here")
    }
}

Or via DialogInterface.OnClickListener:

val cancelListener = DialogInterface.OnClickListener { _, _ -> TODO("Your code here") }

...

.apply {
    setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.dialog_button_cancel), cancelListener)
}

CodePudding user response:

According to @HanifShaikh answer I wrot this code from link that he sent.

  private fun showDatePickerDialog() {
        setDatePickerDialogBorder(true)
        DatePickerDialog(
            requireContext(),
            R.style.DatePickerTheme,
            datePicker,
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),
        ).apply {
            this.setButton(
                    DialogInterface.BUTTON_NEGATIVE, "Cancel"
                ) { _, which ->
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
                        //on cancel click
                        setDatePickerDialogBorder(false)
                    }}
            datePicker.minDate = System.currentTimeMillis() - 1000
        }.show()
    }
  • Related