xamarin android DatePickerDialog set mini date - android

I'm using DatePickerDialog and i want to hide every date before today.
I can hide previous month and year using this code:
dialog.DatePicker.MinDate = new Java.Util.Date().Time - 1000;
But it's not working like i want. Days between 1st of june and today are disabled (gray) but we still can click on them and dismiss the dialog by clicking on the positive button.
Does someone has a solution to realy disabled them ? or at least avoid dismiss the dialog?

You need to give MinDate the number of milliseconds between your minimum date (in your case today) and January 1st 1970, so:
dialog.DatePicker.MinDate = (long)(DateTime.Today.Date - new DateTime(1970, 1, 1)).TotalMilliseconds;
This should prevent users from selecting any earlier dates.

Try changing the mindate to date now?
dateTimePicker1.MinDate = DateTime.Now;
Something like that should work. :)

//for lesser than today
dialog.DatePicker.MinDate = Java.Lang.JavaSystem.CurrentTimeMillis();
//for greater than today
dialog.DatePicker.MaxDate = Java.Lang.JavaSystem.CurrentTimeMillis();
This solved mine :)

Related

How to show the current date of the month in "MaterialDatePicker" in Android?

I've implemented MaterialDatePicker for the purpose of the date selection & also done it but in calendar header initially shows the First date of the current month
Example: Today date is May 30 2020 but in the calendar before select any date i.e initially it shows May 1 2020.
Here is the code & screenshot:
MaterialDatePicker.Builder startDateBuilder;
MaterialDatePicker startDatePicker;
startDateBuilder = MaterialDatePicker.Builder.datePicker();
startDateBuilder.setTitleText("Starting date");
long today = MaterialDatePicker.todayInUtcMilliseconds();
CalendarConstraints.Builder con = new CalendarConstraints.Builder();
CalendarConstraints.DateValidator dateValidator = DateValidatorPointForward.now();
con.setValidator(dateValidator); // Previous dates hide
con.setStart(today); // Calender start from set day of the month
startDateBuilder.setSelection(today);
startDateBuilder.setCalendarConstraints(con.build());
startDateBuilder.setTheme(R.style.MaterialCalendarTheme); // Custom Theme
startDatePicker = startDateBuilder.build();
startDatePicker.show(getSupportFragmentManager(), startDatePicker.toString());
My code is correct it was dependencies version problem. Previously I used the old version, Now update the new version, it was working fine.
com.google.android.material:material:1.2.0-alpha02
after changing the implementation
com.google.android.material:material:1.2.0-beta01
In beta01 also have some bugs in shows the current date in the header of the calendar and also a picker.
It doesn't show the current date in beta01 version so I changed to the stable version Now it will work.
com.google.android.material:material:1.1.0

calendarview highlights wrong week

I have a CalendarView in my app, when the user selects a date by touching that date in the monthview, the correct date is selected (verified by adding debug statements in the code), but the week before is highlighted, so it looks as if the wrong date is selected.
I have found a work-around: if I set 'firstDayInWeek' to 1 the problem is solved, but by default the firstDayInweek is 2 (monday), and then this problem occurs.
Thank you very much!
Samsung S4 with API 21
I have had the same issue as you, using a Samsung S5 running API 21.
There are two workarounds that I have found, none of them is a good experience for our users :(
Force first day of the week to Sunday
calendarView.setFirstDayOfTheWeek(Calendar.SUNDAY);
Set a minimum and a maximum date for the calendar (be careful because not all of the dates work here). I was able to make it work properly setting a minimum date 2 months before the current date and maximum date 2 years after the current date. You can play with these values and find a good compromise between the limits and your user experience.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 2);
calendarView.setMinDate(calendar.getTimeInMillis());
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 2);
calendarView.setMaxDate(calendar.getTimeInMillis());
Unfortunately, this is the only way I could fix this issue, I hope it is useful for you.

Android datepicker setMinDate shows older dates

I want to limit the date selection by setting minDate:
getDatePicker().setMinDate(System.currentTimeMillis() - 500);
Before selection the date picker looks like this:
Today is 30 July, however older dates are still displayed in the dialog. When I try to select an old date it disappears:
As a user I find this strange. Any ideas why this happens? Maybe a bug?

Setting the time for both DatePicker fields in a DatePickerDialog

I am trying to create a DatePickerDialog when a certain TextView is clicked, and then set the date picked to be that TextView. I have gotten this to work, but when the DatePickerDialog is shown, the dates for left DatePicker are correctly set to the current date, while the calendar on the right is set to November 2100. How can I access the field of calendar and set its date?
Here is a link to an image of what I am talking about, but is not from my application exactly.
http://i1023.photobucket.com/albums/af358/shaikhhamadali/typesofdialog_4_zps078711ac.png
So, I am talking about the calendar on the right that does not start on the current date. I would like to know how to access it? If I'm not mistaken, this calendar will only show on tablets, so is there a "safe" way to do this where running it on a phone would not cause any problems?
Here is the code from my DatePickerFragment innerclass onCreateDialog method
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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 datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
datePickerDialog.setTitle("Enter date");
datePickerDialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis() / 86400000L * 86400000L);
return datePickerDialog;
}
Thank you for any help!
UPDATE:
Update: I have tried adding accessing the CalendarView through both, but the CalendarView still starts up at Nov 2100
datePickerDialog.getDatePicker().getCalendarView().setDate(Calendar.getInstance().getTimeInMillis() / 86400000L *86400000L);
datePickerDialog.getDatePicker().getCalendarView().setMinDate(Calendar.getInstance().getTimeInMillis() / 86400000L 86400000L);
Solution
This long answer (to a question about practically the same bug) suggests a workaround for this issue.
Part of it quoted here:
formatDateRange() does not work past 2038.
Workaround #1
class DatePickerDialog1964 extends DatePickerDialog {
DatePickerDialog1964(Context c) {
super(c, null, 2013, 4, 21);
#SuppressWarnings("deprecation")
Date min = new Date(2013-1900, 4, 21);
DatePicker p = getDatePicker();
CalendarView cv = p.getCalendarView(); // should check for null
long cur = cv.getDate();
int d = cv.getFirstDayOfWeek();
p.setMinDate(min.getTime());
cv.setDate(cur + 1000L*60*60*24*40);
cv.setFirstDayOfWeek((d + 1) % 7);
cv.setDate(cur);
cv.setFirstDayOfWeek(d);
}
}
Workaround #2 I actually used
// Calendar view is a cascade of bugs.
// Work around that by explicitly disabling it.
datePicker.setCalendarViewShown(false);
My Deduction
By elimination, we can deduce that the line datePickerDialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis() / 86400000L * 86400000L); is causing the November 2100 error.
(UNIX time / 86400000L) * 86400000L --I guess it's supposed to take advantage of rounding to produce a 'full' day. Beware of it rounding to the same day if the current time is after noon, the previous day if before. It isn't chopping after the floating point.
About the updated part:
Even calling setMinDate then setDate isn't really solving anything -- The problem that caused the Nov 2100 error will persist, because the updated line's effect is executed by the original code.
Then I looked around, and I found the above linked answer.
I guess CalendarView is stuffed with bugs.
There appears to be another NumberPicker bug where the previous month and date and shown sometimes, even if not accessible. They disappear when we try to access them.
P.S: I know this is late, but since the OP didn't provide any solution, an analysis might be helpful to somebody.

How do I set the date for a date picker?

I use DatePickerDialog.OnDateSetListener that's works fine.
I want to add date for 120 days in date picker.
What I mean is if I add 120 days, the date and month will be change automatically.
How to do it?
Something like this should do the trick:
Calendar cal = Calendar.getInstance();
cal.set(datepick.getYear(), datepick.getMonth() + 1, datepick.getDayOfMonth());
cal.add(Calendar.DATE, 120);
datepick.updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) - 1, cal.get(Calendar.DATE));
Make sure you create a date object with 120 days added (see this topic on how to do that) and use that to populate your datepicker, either on initialization or when changed. I'm not really sure what you are trying to achieve however the latter doesn't seem right usability wise. In that case I would create an extra textfield that represents the +120 days date.
Set Date Programmatically by using UpdateDate
datePickerDialog.UpdateDate(selectedDate ?? DateTime.Now);

Categories

Resources