0

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()
Ahmet Yılmaz
  • 57
  • 1
  • 11
  • 1
    I hope it helps you [refrense link](https://stackoverflow.com/questions/2928902/how-do-i-detect-a-cancel-click-of-the-datepicker-dialog) – Hanif Shaikh Dec 09 '22 at 12:56
  • @HanifShaikh thank you. The last comment that solved mine. – Ahmet Yılmaz Dec 09 '22 at 13:08
  • and would happend if user change orinetation when dialog is visible? .... don use Dialogs - use DialogFragments – Selvin Dec 09 '22 at 13:31
  • Thanks for this advice. I will care it. For this project there is no chance to change orientation. It's only for one device with one orientation. – Ahmet Yılmaz Dec 09 '22 at 13:35

2 Answers2

1

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)
}
SerjantArbuz
  • 982
  • 1
  • 12
  • 16
0

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()
    }
Ahmet Yılmaz
  • 57
  • 1
  • 11