Created
March 2, 2014 11:55
-
-
Save lecho/9305505 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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