I want to restrict the range of time in timepicker. For that, I need to set minimum and maximum time in TimePickerDialog Fragment. I have searched many sites for this simple issue, but i didn't any get proper solution. I am getting the solution only for datepicker and number picker. Any help would be appreciate.
and I have used the below code:
public class TimePickerDialogFragment extends DialogFragment {
public static final String TIME_IN_MILLIS = "com.iandmydoctor.android.utils.TIME_IN_MILLIS";
private OnTimeSetListener mTimeSetCallback;
private boolean is24HourView = false;
public TimePickerDialogFragment() {
}
public void setOnTimeSetListener(OnTimeSetListener listener) {
mTimeSetCallback = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle b = getArguments();
Calendar c = Calendar.getInstance();
long time = b.getLong(TIME_IN_MILLIS);
if (time < System.currentTimeMillis()) {
time = System.currentTimeMillis();
}
c.setTimeInMillis(time);
int mHour = c.get(Calendar.HOUR_OF_DAY);
int mMinute = c.get(Calendar.MINUTE);
TimePickerDialog.OnTimeSetListener listener = new TimePickerDialog.OnTimeSetListener() {
boolean fired = false;
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (fired) {
return;
}
fired = true;
if (mTimeSetCallback != null) {
mTimeSetCallback.onTimeSet(hourOfDay, minute);
}
}
};
return new TimePickerDialog(getActivity(), listener, mHour, mMinute, is24HourView);
}
public void setIs24HourView(boolean is24HourView) {
this.is24HourView = is24HourView;
}
public interface OnTimeSetListener {
void onTimeSet(int hourOfDay, int minute);
}
}
and in my activity:
private void showTimePickerDialog(long timeInMillis, TimePickerDialogFragment.OnTimeSetListener listener) {
Bundle args = new Bundle();
args.putLong(TimePickerDialogFragment.TIME_IN_MILLIS, timeInMillis);
TimePickerDialogFragment timePicker = new TimePickerDialogFragment();
timePicker.setArguments(args);
timePicker.setOnTimeSetListener(listener);
timePicker.show(getSupportFragmentManager(), "time_picker");
}