Unfamiliar variable on code

Searched for lambda excplanation

fun clickTimePicker(view: View) {
        val c = Calendar.getInstance()
        val hour = c.get(Calendar.HOUR)
        val minute = c.get(Calendar.MINUTE)

        val tpd = TimePickerDialog(this,TimePickerDialog.OnTimeSetListener(function = {
        view, h, m -> Toast.makeText(this, h.toString() + " : " + m +" : " , 

        Toast.LENGTH_LONG).show()

        }),hour,minute,false)

        tpd.show()
    }

1.what does h and m in that variable mean?

2.what does function = do ?

3.why view is not used?

2 Answers

2
CommonsWare On Best Solutions

Those are the hourOfDay and minute values as seen in the documentation for OnTimeSetListener and its onTimeSet() method.

Your lambda expression forms the body of onTimeSet(), and the three lambda parameters are the three parameters for onTimeSet().

UPDATE: regarding the two additional questions that you added later:

  • function = is not needed there. My guess is that for Kotlin SAM conversions, function is the name given for the lambda that will be converted into the interface's function.

  • As to why view is unused, they did not need it to show the Toast.

0
Chrisvin Jem On

As mentioned in the [official documentation] (https://developer.android.com/reference/android/app/TimePickerDialog.OnTimeSetListener), it refers to the hour Of Day and minute.

view- TimePicker: the view associated with this listener
h   - int: the hour that was set
m   - int: the minute that was set