Santosh
Santosh

Reputation: 611

how to make android time picker to not allow past time being picked

HI am new to android and i want to not allow past time being picked by using time picker.. i tried with the following code

private TimePicker.OnTimeChangedListener StartTimeChangedListener=new TimePicker.OnTimeChangedListener()
    {
        public void onTimeChanged(TimePicker view,int selectedHour,int selectedMinute)
        {
            update(view,selectedHour,selectedMinute);
        }
    };

and i dont know how to write the code for update(view,selectedHour,selectedMinute); can anybody help me please...

Upvotes: 4

Views: 15964

Answers (3)

Edd Perez
Edd Perez

Reputation: 11

if((hourOfDay <= (calendar.get(Calendar.HOUR_OF_DAY)))&&
        (minute <= (calendar.get(Calendar.MINUTE)))){
    Toast.makeText(getActivity(), "Wrong hour",
            Toast.LENGTH_SHORT).show();
}

Upvotes: 1

Ranjit
Ranjit

Reputation: 5150

I modified the code a little and it works for me: I have taken both date and time picker in one alert dialog box and write some code when someone wants to set the time for alarm through its positive button.

       final DatePicker pickdate = (DatePicker) view
                .findViewById(R.id.datePicker1);
       final TimePicker pickTime = (TimePicker) view
                .findViewById(R.id.timePicker1);

        builder.setPositiveButton("Set Alarm Time",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        int day = pickdate.getDayOfMonth();
                        int month = pickdate.getMonth();
                        int year = pickdate.getYear();

                        int hour = pickTime.getCurrentHour();
                        int min = pickTime.getCurrentMinute();
                        c = Calendar.getInstance();

      if (min >= c.get(Calendar.MINUTE)
      && hour >= c.get(Calendar.HOUR_OF_DAY)
      && day >= c.get(Calendar.DAY_OF_MONTH)
      && (month + 1) >= c.get(Calendar.MONTH)
      && year >= c.get(Calendar.YEAR)) {

  String setdate = year + "-" + (month + 1) + "-"
  + day + " " + hour + ":" + min;

  //Use the string where you wants
 }

    else {
         //show some error toast
     Toast.makeText(context,
     Html.fromHtml("<font color=RED><em><strong>Past Time  Can't Be Added</strong></em>/font>")
    ,Toast.LENGTH_SHORT).show();

}

Upvotes: 0

caiocpricci2
caiocpricci2

Reputation: 7798

Have you tried following the tutorial on the developers page for the timepicker?

Edit:

Ok, so following the tutorial we have the update method for the textview that will show your time:

private void updateDisplay() {
    mTimeDisplay.setText(
        new StringBuilder()
                .append(pad(mHour)).append(":")
                .append(pad(mMinute)));
}

private static String pad(int c) {
    if (c >= 10)
        return String.valueOf(c);
    else
        return "0" + String.valueOf(c);
}

The dialog that will be created for the user to pick the time:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case TIME_DIALOG_ID:
        return new TimePickerDialog(this,
                mTimeSetListener, mHour, mMinute, false);
    }
    return null;
}

And here is where I would add the restriction that you want, the callback method when the user sets the time. You need to get the current hour and minute, and if the user selects a past time, you do not display his changes and send him a Toast or something.

Using the Calendar you can easily get it:

Calendar c = Calendar.getInstance();           

// the callback received when the user "sets" the time in the dialog
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
    new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
           if (mHour >= Integer.getInteger(c.get(Calendar.HOUR_OF_DAY)) && mMinute >= Integer.getInteger(c.get(Calendar.MINUTE))) { 
            mHour = hourOfDay;
            mMinute = minute;                
            updateDisplay();
            } else 
            //Display a toast or something to inform the user that he can't pick a past time.   
        }

Now if you want the user to not be able to pick a previous time in the Dialog you created, you have to create your own hour picking screen, because the TimePickerDialog has no restrictions about what values you can pick.

Hope it helps!

Upvotes: 2

Related Questions