user4522568
user4522568

Reputation:

How to set the TimePickerDialog to current Time and show only the current and future times and not the previous times?

I am a newbie to Android. I am trying to work on the TimePickerDialog. I have implemented it and it is working fine, but the only problem is that it is displaying the previous time. Is there any way to show only the current and future time in the TimepickerDialog? I have done the following coding, please guide me.

  TimePickerDialog mTimePicker;

        mTimePicker = new TimePickerDialog(Take_Away.this,
                new TimePickerDialog.OnTimeSetListener() {
                    @SuppressLint("SimpleDateFormat")
                    @Override
                    public void onTimeSet(TimePicker timePicker,
                            int selectedHour, int selectedMinute) {

                        Calendar time = Calendar.getInstance();

                        time.set(Calendar.HOUR_OF_DAY, selectedHour);

                        time.set(Calendar.MINUTE, selectedMinute);
                        SimpleDateFormat format = new SimpleDateFormat(
                                "hh:mm a");
                        et_time.setText(format.format(time.getTime()));
                        hour = selectedHour;
                        minute = selectedMinute;
                    }
                }, hour, minute, false);// Yes 24 hour time
        mTimePicker.setTitle("Select Time");
        mTimePicker.show();

Upvotes: 1

Views: 5315

Answers (4)

Akash Saini
Akash Saini

Reputation: 91

Think this will help mTimePicker.updateTime(hourOfDay,minute);

Upvotes: 0

NAseem Akhtar
NAseem Akhtar

Reputation: 41

Try like below. Hope it will help you

private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {

            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // Show selected date
            editOutputDate.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));

        }`
    };

private int year;
private int month;
private int day;

Upvotes: 0

King of Masses
King of Masses

Reputation: 18765

Use setMinDate() function. It sets the minimal date in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone. Set the minimum date to today's date, so user won't be able to go past it. Something like following in onCreateDialog:

final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog d = new DatePickerDialog(getActivity(), listener, year, month, day);  
DatePicker dp = d.getDatePicker(); 
dp.setMinDate(c.getTimeInMillis());
return d;

Upvotes: 1

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Pass the current date's hour and time values when you construct the TimePickerDialog.

// Get current system time
Calendar currentTime = Calendar.getInstance();

mTimePicker = new TimePickerDialog(Take_Away.this,
        new TimePickerDialog.OnTimeSetListener() {
            @SuppressLint("SimpleDateFormat")
            @Override
            public void onTimeSet(TimePicker timePicker,
                    int selectedHour, int selectedMinute) {

                Calendar time = Calendar.getInstance();

                time.set(Calendar.HOUR_OF_DAY, selectedHour);

                time.set(Calendar.MINUTE, selectedMinute);
                SimpleDateFormat format = new SimpleDateFormat(
                        "hh:mm a");
                et_time.setText(format.format(time.getTime()));
                hour = selectedHour;
                minute = selectedMinute;
            }
        },

        currentTime.get(Calendar.HOUR_OF_DAY), // Current hour value
        currentTime.get(Calendar.MINUTE), // Current minute value
        DateFormat.is24HourFormat()); // Check 24 Hour or AM/PM format

mTimePicker.setTitle("Select Time");
mTimePicker.show();

Notice the use of DateFormat.is24HourFormat() as well. This respects your user's system wide preference for a 24-hour or AM/PM time format.

Upvotes: 1

Related Questions