How to set Maxmum and minimum time in Time picker?

Yugesh picture Yugesh · Jun 4, 2014 · Viewed 7.1k times · Source

i have time interval 12:00 PM to 12:00 AM.I want set this time interval in time picker dialog.Disable the time before 12:00 PM and after 12:00 AM in time picker.I don't know how to set time in time picker dialog.Can any one know help me to solve this issue.

Time Picker

 private void showTimePicker(){

      Time_picker time = new Time_picker();

      Bundle args = new Bundle();
      args.putInt("hours", c.get(Calendar.HOUR_OF_DAY)+2);
      args.putInt("minute", c.get(Calendar.MINUTE)+30);

      time.setArguments(args);
      time.setCallBack(ontime);
      time.show(((FragmentActivity) getActivity()).getSupportFragmentManager(), "Time Picker");

 }

 OnTimeSetListener ontime = new OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub

        try {       
               String _24HourTime = String.valueOf(hourOfDay)+":"+String.valueOf(minute);
               SimpleDateFormat _24HourSDF = new SimpleDateFormat("HH:mm");
               SimpleDateFormat _12HourSDF = new SimpleDateFormat("hh:mm a");
               Date _24HourDt = _24HourSDF.parse(_24HourTime);
               et_time.setText(_12HourSDF.format(_24HourDt));
           } catch (Exception e) {
               e.printStackTrace();
           }
    }
};

Time Picker Dialog Fragment

public class Time_picker extends DialogFragment{

OnTimeSetListener ontimeSet;

public Time_picker() {

}

public void setCallBack(OnTimeSetListener ontime) {

    ontimeSet = ontime;

}

private int hours, minute, pm;
@Override
public void setArguments(Bundle args) {
    super.setArguments(args);
    hours = args.getInt("hours");
    minute = args.getInt("minute");
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    TimePickerDialog _time = new TimePickerDialog(getActivity(), ontimeSet, hours, minute, false){

        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub


        }
    };

    return _time;
}
}

Answer

sola picture sola · Jun 4, 2014

I'm not sure if you can change what time values are presented in the time picker, you can however create an event listener for when the timepicker changes that will reject any values not in the interval you want