Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
public class TimePickerDialogFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
private static final String DIALOG_HOUR = "my.package:INITIAL_HOUR";
private static final String DIALOG_MINUTE = "my.package:INITIAL_MINUTE";
//My custom listener
private OnTimePickedListener mListener;
private int mDialogRequestCode;
//Use dialogRequestCode to discriminate dialogs in OnTimePickedListener#onTimePicked method. I think you could also use dialog tag string instead of integer variable.
public static TimePickerDialogFragment newInstance(int hour, int minute, int dialogRequestCode, OnTimePickedListener listener) {
TimePickerDialogFragment fragment = new TimePickerDialogFragment();
Bundle args = new Bundle();
args.putInt(DIALOG_HOUR, hour);
args.putInt(DIALOG_MINUTE, minute);
mDialogRequestCode = dialogRequestCode;
return fragment;
public void onCreate(Bundle savedInstanceState) {
public Dialog onCreateDialog(Bundle savedInstanceState) {
int hour = getArguments().getInt(DIALOG_HOUR);
int minute = getArguments().getInt(DIALOG_MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (null != mListener) {
mListener.onTimePicked(hourOfDay, minute, mDialogRequestCode);
public OnTimePickedListener getListener() {
return mListener;
public void setListener(OnTimePickedListener listener) {
this.mListener = listener;
public interface OnTimePickedListener {
public void onTimePicked(int hourOfDay, int minute, int dialogRequestCode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.