How to get time time picker from time picker dialog?

1.7k views Asked by At

On back press in date picker and time picker dialog there is known issue in android jellybean and higher that onDateSet() and onTimeSet() methods are called. To overcome this issue i have found a solution for date picker here(https://stackoverflow.com/a/11493752) but the java file(mentioned in 1st point) provided in it has getDatePicker() method for getting date picker from date picker dialog. But there is no such getTimePicker() method for getting time picker. Can anyone help me with this so that time picker also behaves similar to date picker.

Thanks in advance.

1

There are 1 answers

1
Aashish Gulabani On

I did a lot of digging and have found a solution to this and it works fine in my project. In time picker we don't need to get time picker from time picker dialog as it does for date picker by getDatePicker() method. Just create a new Time picker and pass that only to listener in onTimeSet().

final TimePicker tp= new TimePicker(getActivity());
         Calendar cal=Calendar.getInstance();
        final int hour = cal.get(Calendar.HOUR_OF_DAY);
        final int minute = cal.get(Calendar.MINUTE);
        picker.onTimeChanged(tp, hour, minute);