Set Limit on the Datepickerdialog in Android

Set Limit on the DatePickerDialog in Android?

All the other answers seem rather convoluted, so I'm just going to state the obvious: you can get the underlying DatePicker from a DatePickerDialog (by simply calling getDatePicker()) and set its bounds using:

  • setMinDate(long minDate)
  • setMaxDate(long maxDate)

Where the argument is the usual number of milliseconds since January 1, 1970 00:00:00 in the default time zone. You'll still have to calculate these values of course, but that should be trivial to do with the Calendar class: just take the current date and add or substract x years.

The more attentive reader will notice that the aforementioned setters weren't available until API level 11. If you're targeting that (or newer) platform only, then you're good to go. If you also want to support i.e. Gingerbread devices (Android 2.3 / API level 9+), you can use a backported version of DatePicker in stead.

How to set the limit on date in Date picker dialog

You have the setMinDate(long) and setMaxDate(long) methods at your disposal. Both of these will work on API level 11 and above. Since you are using a DatePickerDialog, you need to first get the underlying DatePicker by calling the getDatePicker() method.

dpdialog.getDatePicker().setMinDate(minDate);  
dpdialog.getDatePicker().setmaxDate(maxDate);

Source :Set Limit on the DatePickerDialog in Android?

You can calculate the minDate by using,

Date today = new Date();
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add( Calendar.MONTH, -6 ) // Subtract 6 months
long minDate = c.getTime().getTime() // Twice!

Updated :

Replace the below line

return new DatePickerDialog(getActivity(), this, year, month, day);

with

 // Create a new instance of DatePickerDialog and return it
DatePickerDialog pickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
pickerDialog.getDatePicker().setMaxDate(maxDate);
pickerDialog.getDatePicker().setMinDate(minDate);
return pickerDialog;

Set date limit in DatePickerDialog android

You have the setMinDate(long) and setMaxDate(long) methods at your disposal. Both of these will work on API level 11 and above. Since you are using a DatePickerDialog, you need to first get the underlying DatePicker by calling the getDatePicker() method.

dpdialog.getDatePicker().setMinDate(minDate);  
dpdialog.getDatePicker().setmaxDate(maxDate);

Source: Set Limit on the DatePickerDialog in Android?

Edit:

Date today = new Date();
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add( Calendar.MONTH, -6 ) // Subtract 6 months
long minDate = c.getTime().getTime() // Twice!

Source: java.util.Date - Deleting three months from a date?

How set maximum date in datepicker dialog in android?

Use setMaxDate().

For example, replace return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay) statement with something like this:

    DatePickerDialog dialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;

Android DatePickerDialog: Set min and max date for selection

Try this method

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dialog = new DatePickerDialog(getContext(), listener, year, month, day);
Field mDatePickerField;
try {
mDatePickerField = dialog.getClass().getDeclaredField("mDatePicker");
mDatePickerField.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
return dialog;
}

instead of your

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dialog = new DatePickerDialog(getContext(), listener, year, month, day);
dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
return dialog;
}

EDIT1:

I have also faced this issue that user can select not-selectable dates in Android L 5.0.2. Currently there is bug reported here. It is solved in Android L 5.1.0.

For temporary solution of this issue you can compare selected date with current system date and put some condition based on that. I used this as my workaround

EDIT2:

add onDateSent() method in DatePickerDialogFragment and just check if it's earlier than the date you set in setMinDate(). If so, then just show the DatePickerDialog again.

final long today = System.currentTimeMillis() - 1000;

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
//If user tries to select date in past (or today)
if (calendar.getTimeInMillis() < today)
{
//Make them try again
DatePickerDialogFragment fragment = new DatePickerDialogFragment();
fragment.setListener(dateSetListener);
fragment.show(getSupportFragmentManager(), "Choose booking date");
Toast.makeText(this, "Invalid date, please try again", Toast.LENGTH_LONG).show();
}
else
{
//success
}
}

How to set a minimum and maximum limit for a date picker in android?

Try this, This shows from today to next 5 year date in datepicker dialog.

public class MainActivity extends Activity {

TextView txtDatePicker;
Calendar cal,cal1;
long maxDate;
Date date;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

cal = Calendar.getInstance();
cal1 = Calendar.getInstance();

txtDatePicker = (TextView) findViewById(R.id.txtDatePicker);
txtDatePicker.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, new OnDateSetListener() {

@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, ""+arg1+"/"+(arg2+1)+"/"+arg3, Toast.LENGTH_SHORT).show();
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMinDate(System.currentTimeMillis());
cal1.add(Calendar.YEAR, 5);
dialog.getDatePicker().setMaxDate(cal1.getTimeInMillis());
dialog.show();
}
});
}
}

how to set max timepicker limit based on datepicker android studio

You can check selected date is Saturday or not using below code:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);// you have increased value of monthOfYear by using ++monthOfYear so you have to use monthOfYear-1
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
isSaturday = cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY;

and pass this boolean to your timePicker(false, isSaturday) function. In timePicker function you can set maxHours:

int maxHour = 15;
if(isSaturday){
maxHour=12
}


Related Topics



Leave a reply



Submit