How to show Material Calendar with all disabled dates in Android datePickerDialog - android

I am trying to make a calendar date picker dialog pop-up with a condition. User selects month and year at first. Hence, DatePickerDialog pop-up will show with minimum 28 days (for February only) and maximum 31days (for January, March, May, …, December). User is able to pick particular date in a previously selected month of the year. Thus, those dates on which user gave attendance are set enabled, rest dates are set disabled. Now, there may be a situation that user was absent for the whole month. I want to show all the dates set disabled in the calendar for that particular month of the year. I was looking at the documentation that there were methods called setSelectableDays(Calendar[] days) and setDisabledDays(Calendar[] days), both takes #NonNull array of calendar objects to enable and disable dates repectively.
So, for this case if there was no attendance date for a particular month, I tried to make a absentDates array of calendar and passed it to the second method i.e. setDisabledDays(Calendar[] days). But, it not showing anything. I tried and checked that except one day in a specific month, I can disable rest days, not all the dates at once (an image is attached of that). I want all the dates disabled in the DatePickerDialog pop-up.

If you use the official MaterialDatePicker you can implement your own DateValidator to enable/disable days and setting it to the CalendarConstraints object before building the dialog with MaterialDatePicker.Builder().datePicker().setCalendarConstraints(contraints)
You can find a sample implementation here for DateValidatorPointForward.
Documentation: https://material.io/components/date-pickers/android#using-date-pickers

Related

DatePickerDialog resets to default month when changing selectableDays

I have a DatePickerDialog (com.wdullaer.materialdatetimepicker.date) that has its selectableDays populated from an API.
The API that we use only supports retrieving one month at a time. As a result, we need to retrieve more selectableDays for the next month when the user taps the arrow to go to the next month. (Circled in red in the attached image)
We accomplish this by keeping a global list of calendars and adding additional Calendars from the network responses:
val selectableCalendars = HashSet<Calendar>() // List of available appointment dates (including time)
That works fine, but when DatePickerDialog is open and the following line runs to change the selectable days:
datePickerDialog.selectableDays = selectableCalendars.toTypedArray()
The DatePickerDialog will jump back to the first month in the calendar list.
This "jump back to June" (unintended behavior) always occurs under the following circumstances:
1.) Immediately after we set datePickerDialog.selectableDays to a new value.
2.) Only if there are no selectable days for the month the user scrolled into. (Bug does not occur if there are selectable days in the
returned month!)
Does anyone have a workaround for this issue?
By checking the code here
https://github.com/wdullaer/MaterialDateTimePicker/blob/f849a5c2704c974ba182fe4e2e205fa7f4fd395d/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DatePickerDialog.java#L846
It seems that after you set new selectableDays, the refresh function will take current selected day position and scroll back to it https://github.com/wdullaer/MaterialDateTimePicker/blob/f849a5c2704c974ba182fe4e2e205fa7f4fd395d/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DayPickerView.java#L142
So my proposal will be setting the mSelectedDay to the first day of the month when user scoll to the next month. Which I think also not a great user experience.
I would also recommend to post this question on author's github page, or search for similar question/issue/bug history in the library github pag

Selecting some dates in Calendar while initializing it

I am using CalenderView in android. I have a list of predefined dates as ["2019-02-15","2019-02-16","2019-02-17"]. When Calender is initialized and shown on Activity/fragment i want this dates to be selected.How to achieve this.
I am initializing calender this way
calender=v.findViewById(R.id.cal);
calender.setMinDate(System.currentTimeMillis()-1000);
How to achieve this ?
Looking into the Android API Reference you find this function:
CalendarView.setDate(long date)
Sets the selected date in milliseconds since January 1, 1970 00:00:00 in TimeZone.getDefault() time zone. https://developer.android.com/reference/android/widget/CalendarView.html#setDate(long)
So in your case you should call:
calender.setDate(long date);
In order to get the milliseconds, you should first parse your date string and then convert them to milliseconds.
Additionally I am not sure and I dont't think that it is possible to select more than one date at a time.
You cannot disable specific dates in the default CalendarView of android, for that you've to fork that widget and do your customisation. Then the other option is to use third party libraries like
material-calendarview
Using this library you can easily disable custom dates and apply custom background color and so on.
Material-Calendar-View
This is also other third party option which provides multiple date selection and so on.

Android CalendarView how to detect Month change

I need to search my database basing the query on the month returned from a CalendarView. My problem is that onSelectedDayChange() event fires only when clicking, actually selecting a date not when the month is changed by swiping CalendarView.
How to set up something like "onSelectedMonthChange"?
You can't do it with the Android CalendarView. Look at this thread.
If you want to do it, you will have to make custom CalendarView class which will extend CalendarView and implement onGestureListener. Keep a variable curMonth to keep account of your month. Whenever swipe left/right happen, update your month variable accordingly.
if you want to show next and previous months in your calendar based on some click event, then you can do it as:
Calendar c = Calendar.getInstance();
//for next month
btnNext.onClick(){
c.add(Calendar.MONTH,1);
}
//previous month
btnPrevious.onClick(){
c.add(Calendar.MONTH,-1);
}

Android DatePickerDialog to a year of history

I'm coding an android app about the egyptian history, and I want to add a DatePicker in a dialog, where I will show years, and whenever the user sets a year, it shows in the layout the events in that year . If he changes the year, the event changes ...etc
It is a historical app, and I don't know how to do it!
I need help.
According to the doc (http://developer.android.com/reference/android/widget/DatePicker.html)
You can set min and max date according to the era you want to cover:
android:maxDate The minimal date shown by this calendar view in mm/dd/yyyy format.
android:minDate The minimal date shown by this calendar view in mm/dd/yyyy format.
And then use the onDateChangedListener to display the correct Layout.
http://developer.android.com/reference/android/widget/DatePicker.OnDateChangedListener.html
As you want to use only a year, I would advise to use a NumberPickerDialog instead.
DatePicker is maybe "too much" for your use case.

how to get next day of current date in a MonthDisplayHelper in android?

i have a monthdisplayhelper to display the month, i want to select days between two date. i.e.,if i selected 2nd July and 9th July, all the dates(3rd to 8th) in between these dates must be selected or highlighted.
You can use the Calendar class. Initialise a new Calendar object for your initial date, then use [calendar instance].add(Calendar.DAY_OF_YEAR, 1) to increment the date by 1 day, it'll automatically roll to the next month if required.
Calendar reference: http://developer.android.com/reference/java/util/Calendar.html
If you know how many days you will need then you could have a loop basically add days and store in a list

Categories

Resources