How to set max and min time in Time picker

In this android programming source code example, we are going to set Maxmum and minimum time in Time picker.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to set Maxmum and minimum time in Time picker.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

        android:textStyle="bold" />


import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.util.Calendar;

public class DatePickerAndTimePickerActivity14 extends AppCompatActivity {
    private int hour, minute;

    protected void onCreate(Bundle savedInstanceState) {
        //setting default time
        hour = 10;
        minute = 20;

    public void ShowTimePicker(View view){
        RangeTimePickerDialog tpd = new RangeTimePickerDialog(DatePickerAndTimePickerActivity14.this, new RangeTimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
        }, hour, minute, true);
        tpd.setMin(11, 20);
        tpd.setMin(14, 20);;

    public class RangeTimePickerDialog extends TimePickerDialog {

        private int minHour = -1;
        private int minMinute = -1;

        private int maxHour = 25;
        private int maxMinute = 25;

        private int currentHour = 0;
        private int currentMinute = 0;

        private Calendar calendar = Calendar.getInstance();
        private DateFormat dateFormat;

        public RangeTimePickerDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) {
            super(context, callBack, hourOfDay, minute, is24HourView);
            currentHour = hourOfDay;
            currentMinute = minute;
            dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
            fixSpinner(context, hourOfDay, minute, is24HourView);

            try {
                Class<?> superclass = getClass().getSuperclass();
                Field mTimePickerField = superclass.getDeclaredField("mTimePicker");
                TimePicker mTimePicker = (TimePicker) mTimePickerField.get(this);
            } catch (NoSuchFieldException e) {
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {

        public void setMin(int hour, int minute) {
            minHour = hour;
            minMinute = minute;

        public void setMax(int hour, int minute) {
            maxHour = hour;
            maxMinute = minute;

        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

            boolean validTime = true;
            if (hourOfDay < minHour || (hourOfDay == minHour && minute < minMinute)){
                validTime = false;

            if (hourOfDay  > maxHour || (hourOfDay == maxHour && minute > maxMinute)){
                validTime = false;

            if (validTime) {
                currentHour = hourOfDay;
                currentMinute = minute;

            updateTime(currentHour, currentMinute);
            updateDialogTitle(view, currentHour, currentMinute);

        private void updateDialogTitle(TimePicker timePicker, int hourOfDay, int minute) {
            calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            calendar.set(Calendar.MINUTE, minute);
            String title = dateFormat.format(calendar.getTime());

        private void fixSpinner(Context context, int hourOfDay, int minute, boolean is24HourView) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // android:timePickerMode spinner and clock began in Lollipop
                try {
                    // Get the theme's android:timePickerMode
                    //two modes are available clock mode and spinner mode ... selecting spinner mode for latest versions
                    final int MODE_SPINNER = 2;
                    Class<?> styleableClass = Class.forName("$styleable");
                    Field timePickerStyleableField = styleableClass.getField("TimePicker");
                    int[] timePickerStyleable = (int[]) timePickerStyleableField.get(null);
                    final TypedArray a = context.obtainStyledAttributes(null, timePickerStyleable, android.R.attr.timePickerStyle, 0);
                    Field timePickerModeStyleableField = styleableClass.getField("TimePicker_timePickerMode");
                    int timePickerModeStyleable = timePickerModeStyleableField.getInt(null);
                    final int mode = a.getInt(timePickerModeStyleable, MODE_SPINNER);
                    if (mode == MODE_SPINNER) {
                        TimePicker timePicker = (TimePicker) findField(TimePickerDialog.class, TimePicker.class, "mTimePicker").get(this);
                        Class<?> delegateClass = Class.forName("android.widget.TimePicker$TimePickerDelegate");
                        Field delegateField = findField(TimePicker.class, delegateClass, "mDelegate");
                        Object delegate = delegateField.get(timePicker);
                        Class<?> spinnerDelegateClass;
                        if (Build.VERSION.SDK_INT != Build.VERSION_CODES.LOLLIPOP) {
                            spinnerDelegateClass = Class.forName("android.widget.TimePickerSpinnerDelegate");
                        } else {

                            spinnerDelegateClass = Class.forName("android.widget.TimePickerClockDelegate");
                        if (delegate.getClass() != spinnerDelegateClass) {
                            delegateField.set(timePicker, null); // throw out the TimePickerClockDelegate!
                            timePicker.removeAllViews(); // remove the TimePickerClockDelegate views
                            Constructor spinnerDelegateConstructor = spinnerDelegateClass.getConstructor(TimePicker.class, Context.class, AttributeSet.class, int.class, int.class);
                            // Instantiate a TimePickerSpinnerDelegate
                            delegate = spinnerDelegateConstructor.newInstance(timePicker, context, null, android.R.attr.timePickerStyle, 0);
                            delegateField.set(timePicker, delegate); // set the TimePicker.mDelegate to the spinner delegate
                            // Set up the TimePicker again, with the TimePickerSpinnerDelegate
                } catch (Exception e) {
                    throw new RuntimeException(e);
        private Field findField(Class objectClass, Class fieldClass, String expectedName) {
            try {
                Field field = objectClass.getDeclaredField(expectedName);
                return field;
            } catch (NoSuchFieldException e) {} // ignore
            // search for it if it wasn't found under the expected ivar name
            for (Field searchField : objectClass.getDeclaredFields()) {
                if (searchField.getType() == fieldClass) {
                    return searchField;
            return null;

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.


Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

50% Discounts On Projects

Do you have a custom project with a clear requirements, we are currently giving a 50% discount on all projects.

We will work with you to ensure we deliver what you need.

If you have questions or want to chat with us, you can reach us on Skype (henry.tochi)

Languages and technologies we use - Java, Kotlin, Laravel, Vue, React, Flutter, Swift and more.