Challenge: TimeDialoge

This method is without a new fragment… it shows time picker dialog directly…

I created a button on the view…

 <Button
        android:id="@+id/crime_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="12:34 PM"/>

defined it in CrimeFragment and added a listener to open the dialog…

crimeTime.setOnClickListener {
            val calendar = Calendar.getInstance()
            val timeListener = TimePickerDialog.OnTimeSetListener { timePicker: TimePicker , hourOfDay, minute ->
                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay)
                calendar.set(Calendar.MINUTE, minute)
                crimeTime.text = java.text.SimpleDateFormat("HH:mm").format(calendar.time)
            }
            TimePickerDialog(context, timeListener, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), false).show()
        }

there you go…

1 Like