0

I have code that calls upon the launching of a DatePickerFragment and TimePickerFragment when buttons are pressed within the activity's main class.

 fun showDatePickerDialog(view: View) {
        val newFragment = DatePickerFragment()
        newFragment.show(supportFragmentManager, "datePicker")
    }

    fun showTimePickerDialog(view: View) {
        TimePickerFragment().show(supportFragmentManager, "timePicker")
    }

Next, I have used this guide from the docs to initiate this process - which involves the classes class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener and class TimePickerFragment : DialogFragment(), TimePickerDialog.OnTimeSetListener. This is where my problem has arisen. This is what I was trying to do:

class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        // Use the current date as the default date in the picker
        val c = Calendar.getInstance()
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH)
        val day = c.get(Calendar.DAY_OF_MONTH)

        // Create a new instance of DatePickerDialog and return it
        return DatePickerDialog(activity, this, year, month, day)
    }

    override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
        // Do something with the date chosen by the user
        var dateText = findViewbyId<textview>(R.id.text_newRem_date)
        dateText.text("$day/$month/$year")
    }
}

Simply, I wanted to edit the text in the text_newRem_date TextView to reflect the date selected for the user, the same applies for the time but the process would be exactly the same. I am unfamiliar with the way that classes work, so is there a better way to do this? Effectively, I am looking to pass data between each class.

I'm new to Kotlin dev, please forgive me if this is something really basic!

Noren
  • 30
  • 6

0 Answers0