android: TimePickerDialog prevent user select past time and can select future time with new date
Asked Answered
T

3

9

I am using this link Android TimePickerDialog set max time.

I am new in android. With the help of this code, I cannot select past time but we cannot select future time. When 12 is selected in timepickerdialog mode change to am automatically according to the next day not past day.

Turne answered 6/1, 2017 at 13:24 Comment(5)
I am new in android. I am working on this task.Turne
First read this link stackoverflow.com/help/how-to-ask. In current situation it is completely unclear what you want to accomplish and what is your problem. So first edit your question according to the link and make it more clear and preciseOrcein
@VivekMishra He asked and this question is normal question. The main problem that TimePicker in TimePickerDialog don't change current hour and minutes after onTimeChanged.Seaward
@AtifAmin so u want to disable all past time form timepicker?Mutton
@Mutton yes , but they can select future time with new date??Turne
M
21

try this code:

TimePickerFragment timePickerFragment = new TimePickerFragment();
            timePickerFragment.setOnTimeSetListener(new OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    Calendar datetime = Calendar.getInstance();
                    Calendar c = Calendar.getInstance();
                    datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    datetime.set(Calendar.MINUTE, minute);
                    if (datetime.getTimeInMillis() >= c.getTimeInMillis()) {
                        //it's after current
                         int hour = hourOfDay % 12;
                    btnPickStartTime.setText(String.format("%02d:%02d %s", hour == 0 ? 12 : hour,
                            minute, hourOfDay < 12 ? "am" : "pm"));
                    } else {
                        //it's before current'
                        Toast.makeText(getApplicationContext(), "Invalid Time", Toast.LENGTH_LONG).show();
                    }
                }
            });
            timePickerFragment.show(getSupportFragmentManager(), "TIME");

it will show the timepicker dialog and if user select any time before current time... it will show invalid time..

EDIT You need to import:

import android.app.TimePickerDialog;
import android.app.Fragment;
import android.app.DialogFragment;
import java.util.Calendar;
import android.widget.TimePicker;

Here is a Good Example

Mutton answered 6/1, 2017 at 14:39 Comment(3)
what is time picker fragment here , where is the code snippet @MuttonRaddled
@Raddled i have edited the answer to include the import statementsMutton
thanks a lot @rafsanahmad007, but i have done it another wayRaddled
S
7

The best solution for you will be to use this material design library to pick time or date and set min value.

    TimePickerDialog tpd = TimePickerDialog.newInstance(
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
                //put some interesting code
            }
        },
        now.get(Calendar.HOUR_OF_DAY),
        now.get(Calendar.MINUTE),
        true
    );
    tpd.setMinTime(10, 0, 0); // MIN: hours, minute, secconds
    tpd.show(getFragmentManager(), "TimePickerDialog");

The easiest way to add the Material DateTime Picker library to your project is by adding it as a dependency to your build.gradle

dependencies {
    compile 'com.wdullaer:materialdatetimepicker:3.0.0'
}

Hope this will help you.

Seaward answered 6/1, 2017 at 15:20 Comment(0)
B
1
private int  mHour, mMinute;
final Calendar c = Calendar.getInstance();

mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(ActivityScheduleTransfer.this,
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                final Calendar checkOldDate = Calendar.getInstance();
                  if (hourOfDay >= checkOldDate.get(Calendar.HOUR_OF_DAY)) { 
                      if (hourOfDay == checkOldDate.get(Calendar.HOUR_OF_DAY) && minute <= checkOldDate.get(Calendar.MINUTE)) {
                                return;
                        }
                    //select current after 
                } else {`enter code here`
                    //select current before 
                }
            }
        }, mHour, mMinute, false);
            timePickerDialog.show();
Bootjack answered 9/4, 2022 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.