Skip to content

Instantly share code, notes, and snippets.

Embed
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);
fragment.setArguments(args);
fragment.setListener(listener);
mDialogRequestCode = dialogRequestCode;
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme);
}
@Override
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()));
}
@Override
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.