How to stop Date Dialer Dialog Date for setting previous dates. It should only allow above the current date of the current system

Hi everyone, thanks in advance. I have a requirement in my application that I have a Date Sensor to set the date. Now I want to limit the date selection to set the date above System current dates, but not lower than the current System date. How....? Can someone help me on this ... And how to check the current system date with the date of the date selection.

+4
source share
3 answers

In this case, you can extend the DatePickerDialog and make your own implementation of OnDateChanged , which is called every time the date changes, and you get DatePicker parameters as well as the values ​​of the new year, month and day, so you can check if this date has passed, and in this case throw an error (using Toast or something else) and call DatePicker.updateDate () to set the correct value (so that DatePicker is always in a consistent state).

Alternatively, you can call DatePicker.init(year, monthOfYear, dayOfMonth, onDateChangedListener); then you can pass

 onDateChangedListener implementation without having to extend DatePickerDialog. 

EDIT: (I never try to do this, but I think it can do your work ..)

Datepicker

 setMinDate(long minDate) 

Sets the minimum date that this NumberPicker supports in milliseconds since January 1, 1970, 00:00:00 in the getDefault () time zone.

Example:

 DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday); dialog.getDatePicker().setMinDate(new Date()); 
+3
source

Use the setMinDate function of the CalendarView class. Here you can set the date in milliseconds. To prevent future dates, use setMaxDate

Use getDatePicker to get datepicker and set calendarView as above.

+2
source
  private int myear; //Declare these three variables in MainActivity and call showDialog(1) private int mmonth; private int mday; final Calendar myCalendar= Calendar.getInstance(); private void setCurrentDateOnView() { //Call this method before showDialog myear = myCalendar.get(Calendar.YEAR); mmonth = myCalendar.get(Calendar.MONTH); mday = myCalendar.get(Calendar.DAY_OF_MONTH); } protected Dialog onCreateDialog(int id) { switch (id) { case 1: DatePickerDialog startDate = new DatePickerDialog(this, datePickerListener, myear,mmonth, mday){ @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { } }; myCalendar.set(myear, mmonth, mday); long startTime = myCalendar.getTimeInMillis(); startDate.getDatePicker().setMinDate(startTime - 1000); return startDate; } return null; } 
+1
source

Source: https://habr.com/ru/post/1390178/


All Articles