Multiple Date Picker Android Kotlin

  android, datepicker, kotlin

I’m a beginner who started to learn about date-picker in android kotlin, my date-picker works well. But I need to make multiple date-picker in my project and i don’t know how to do that.

MainActivity.kt

class MainActivity : AppCompatActivity(), DatePickerDialog.OnDateSetListener {

var day = 0
var month = 0
var year = 0

var savedDay = 0
var savedMonth = 0
var savedYear = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    pickDate()
}

private fun getDateTimeCalender() {
    val cal = Calendar.getInstance()
    day = cal.get(Calendar.DAY_OF_MONTH)
    month = cal.get(Calendar.MONTH)
    year = cal.get(Calendar.YEAR)
}

private fun pickDate() {
    pickButton.setOnClickListener {
        getDateTimeCalender()

        DatePickerDialog(this,this,year,month,day).show()
    }
}

override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
    savedDay = dayOfMonth
    savedMonth = month + 1
    savedYear = year

    txtDate.text = "$savedDay-$savedMonth-$savedYear"
}
}

Please help me solve this problem. Thanks anyway.

Source: Android Questions

LEAVE A COMMENT