7

How to control TimePicker, I just want to show time range in time picker from 08:00 am to 02:00 pm (except from 08:00 am to 02:00 pm hide all)

I have written complete code to show TimePicker, and customized time as well.

Here is my code:

         case DIALOG_TIME:
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);

            // Create a new instance of TimePickerDialog and return it
            return new TimePickerDialog(this, lisTime, hour, minute,
            DateFormat.is24HourFormat(MainActivity.this));

        TimePickerDialog.OnTimeSetListener lisTime = new TimePickerDialog.OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub
            String meridiem = "";
            nim = minute;
            Calendar datetime = Calendar.getInstance();
            datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
            datetime.set(Calendar.MINUTE, minute);

            if (datetime.get(Calendar.AM_PM) == Calendar.AM)
                meridiem = "AM";
            else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
                meridiem = "PM";

            hour = (datetime.get(Calendar.HOUR) == 0) ?"12":String.valueOf(datetime.get(Calendar.HOUR));

            String time = pad(Integer.parseInt(hour)) + ":" + pad(minute) + " " + meridiem;
            editTime.setText(time);

        }
    };

    private String pad(int value){

        if(value<10){
            return "0"+value;
        }

        return ""+value;
    }
Sun
  • 6,768
  • 25
  • 76
  • 131
  • @sasikumar i don't know how to limit time in timepicker ? still i have no control on it... showing all 24 hours and i just want to show 08:00 am to 02:00 pm – Sun Sep 19 '15 at 10:34
  • As far as I understand it, there is no way of limiting the timee range of the `TimePicker` widget. You would have to create your own. – Squonk Sep 19 '15 at 10:35
  • its simple refer http://stackoverflow.com/questions/20214547/show-timepicker-with-minutes-intervals-in-android – sasikumar Sep 19 '15 at 10:37
  • @sasikumar I know about the minute intervals but don't have any idea about limiting time range .... – Sun Sep 19 '15 at 10:39
  • @Squonk can you show me the way ? – Sun Sep 19 '15 at 10:39
  • 2
    @Sun : I don't understand how `TimePicker` works so I wouldn't know where to start. You could just use two `Spinner` widgets side-by-side and set the contents of the left one from 08 -> 14 and the right one from 0 -> 59. – Squonk Sep 19 '15 at 10:54
  • You need to customize the time picker...have a look at this http://stackoverflow.com/questions/13516389/android-timepickerdialog-set-max-time – WISHY Oct 09 '15 at 06:18
  • @WISHY i already did not get any success – Sun Oct 09 '15 at 06:27
  • This might Help....http://stackoverflow.com/questions/13516389/android-timepickerdialog-set-max-time – Burhanuddin Rashid Oct 09 '15 at 12:56

1 Answers1

5

As in this answer: https://stackoverflow.com/a/20396673/4130107 You can create your custom TimePickerDialog and overwrite the onAttachedToWindow(); but instead limit the hour range:

private final boolean mIs24HourView;

public CustomTimePickerDialog(Context context, OnTimeSetListener callBack,
                              int hourOfDay, int minute, boolean is24HourView) {
    super(context, callBack, hourOfDay, minute, is24HourView);
    mIs24HourView = is24HourView;
}

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    try {
        Class<?> classForid = Class.forName("com.android.internal.R$id");
        Field timePickerField = classForid.getField("timePicker");
        this.timePicker = (TimePicker) findViewById(timePickerField
                .getInt(null));
        Field field = classForid.getField("hour");


        final NumberPicker mHourSpinner = (NumberPicker) timePicker
                .findViewById(field.getInt(null));

        if (mIs24HourView) {
            mHourSpinner.setMinValue(2);
            mHourSpinner.setMaxValue(20);
        }else {
            Field amPm = classForid.getField("amPm");

            mHourSpinner.setMinValue(2);
            final NumberPicker amPm = (NumberPicker) timePicker
                    .findViewById(amPm.getInt(null));
            amPm.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                @Override
                public void onValueChange(NumberPicker np1, int oldVal, int newVal) {
                    if (newVal == 0) { // case AM
                        mHourSpinner.setMinValue(2);
                        mHourSpinner.setMaxValue(12);
                    } else { // case PM
                        mHourSpinner.setMinValue(1);
                        mHourSpinner.setMaxValue(8);
                    }
                }
            });

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

EDIT: now working for AM/PM too

enter image description here

Community
  • 1
  • 1
Tobi
  • 924
  • 1
  • 10
  • 39