Welcome to TimePickerDialog Android Tutorial. In this tutorial we are going to create TimePickerDialog and choose time for our TextView.
TimePickerDialog is a class that extends AlertDialog class. TimePicker let’s user choose time, in this case when we choose time and click on “Ok” our TextView updates.
First step is to create layout, for that we just need one Button(for opening TimePickerDialog) and one TextView(for diplaying choosen time).
Now when we are done with layout we can safely jump into coding. Next step is to of course declare and initialize Button and TextView. Now when we finish that we need to set onClickListener for our Button.
When user clicks on that button we want TimePicker to appear. Now let’s create TimePickerDialog object. First parameter is context, second one is TimePickerDialog.onTimeSetListener. We want to Override method of onTimeSetListener that’s called onTimeSet because that is a method that gives us choosen time in Time Picker. Here we update our TextView.
Third parameter is hour, 4th is minute and last one is displayed format for Time Picker – is it 24 hour format or 12h hour format.
Last thing we ought to do is to call show() function of our TimePickerDialog class.
For more tutorials subscribe to Programming Hustle channel