Code example for TimePicker

Methods: setCurrentHoursetCurrentMinutesetOnTimeChangedListener

0
            @Override 
            public void onTimeChanged(TimePicker timePicker, int hour, int minute) {
                /** 
                 * Hack to prevent StackOverflowError 
                 */ 
                timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
                    @Override 
                    public void onTimeChanged(TimePicker timePicker, int hour, int minute) {
                    } 
                }); 
 
                if ( minute == 59 ) {
                    minute = 60 - step;
                    if (hour > lastHour && lastMinute == 0) {
                        --hour;
                    } 
                } else if ( minute == 60 - step + 1 ) {
                    minute = 0;
                    ++hour;
                } else if ( minute > lastMinute ) {
                    minute = lastMinute + step;
                } else if ( minute < lastMinute ) {
                    minute = lastMinute - step;
                } 
 
                if ( hour <= minHour || hour == 23 ) {
                    hour = minHour;
                    if ( minute <= minMinute) {
                        minute = minMinute;
                    } 
                } else if ( hour >= maxHour) {
                    hour = maxHour;
                    minute = maxMinute;
                } 
 
                lastMinute = minute;
                lastHour = hour;
 
                timePicker.setCurrentMinute(minute);
                timePicker.setCurrentHour(hour);
                timePicker.setOnTimeChangedListener(this);
            } 
        }); 
 
        return timePicker;
    }