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
Related
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
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);
}
I have a record with date and time, and I would want to convert the transaction date from current date, differentiate and show number of days.
For e.g.
If the the record has got a date time, which is 1 hours before than Now, the system should show an hour ago...
Similarly if there is a record with 3 days before then instead of showing a date, I would want to show "3 days ago".
No need to use any custom solution.
You can use android.text.format.DateUtils.getRelativeTimeSpanString
It returns a string describing 'time' as a time relative to 'now'.
reference: http://developer.android.com/reference/android/text/format/DateUtils.html
I'm really stuck with a certain problem and I'm hoping someone can help me understand the problem and come to a solution. I've looked online a fair bit but can't see an answer unless it's been staring me in the face :-/
Basically, I'm creating a very basic TV Guide app. It parses data from an RSS feed which has days offset (yesterday was -1. today is 0, tomorrow is 1, etc etc) and I'm trying to implement a DatePicker that allows the user to see what is on a particular channel when they select yesterday, today, tomorrow, etc.. but if they pick a date that is out-with the range (at the moment it's a week in advance), a simple Toast message will be displayed.
My questions I guess are, firstly, how do I use maybe an IF ELSE to either parse the specific channel data for the day the user wants or display an error Toast message, and, how do I go about converting the days from what the user has put in compared to the actual date today into integers? If they select yesterday's date it will go to URL "http://example.com/-1/channel", if they select tomorrow's date it will go to URL "http://example.com/1/channel" etc etc etc.
Code is available if anyone needs to see it, but I think if someone would be kind enough to explain the logic, I'd like to see if I can come to the answer myself...
Thanks a lot folks!!
You should use a DatePicker to allow the user to choose the when.
Time in Android is stored on a long (not an int). And the long time can easily be converted back and forth between long (always milli-seconds) and a Date object.
The Date object gives you all sorts of tools to compare before and after, look at months, minutes, hours, etc.
The current time is determined by:
long nowMs = System.currentTimeMillis();
int nowSec = (int)(nowMs / 1000);
There is also a very important Calendar object. This allows you to parse textual date formats as delivered by your http functions in and out of various dates.
For example:
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss Z");
String text = sdf.format(cal.getTime();
You will have to put all these tools together with a DatePicker example such as the one here Create a DatePicker to complete your TV Guide application.
Reference:
Date
Calendar
DatePicker
EDIT : Check David's Answer its better.
First Filter the date selected with today's date. You can compare it by date.isbefore(date) or date.isafter(date) these booleans will let you tell know if a date is of past or future or present. then to further calculate the days inbetween you can make a method with switch statement that will basically convert the selected date and the current date into miliseconds(Date.getTimeinmiliseconds)
if the date is of past take the difference of present time in miliseconds and past date in miliseconds. If the date is of future do the opposite. Take the difference and convert it to days difference with appropriate sign(negative/positive).
Please refer this link for a better coding example
I am basically using a datetime control in my application.
Wherein on a click event i am initiating datetime dialog.
In another scenario, i want to move current date, to next day, or previous day.
I don't want the dialogbox of date time control to be displayed.
Is it possible??
The DatePicker and TimePicker controls are available as widgets for you to use in your layouts or code.
You can then build the next day / previous day functionality yourself by setting the date / time of those controls.