Android CalendarView how to detect Month change - android

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);
}

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

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

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

Calendar in Android app to select month and year

I want to have a calendar that would allow the user to only choose month and year. Are there any 3rd party widgets that would allow me to do that? I know about date slider (http://i.stack.imgur.com/BDFls.png) that allows me to create a dialog box where user would choose month and year, but is there a widget that is similar to what I posted below? Or would I have to create this myself?
I would recommend CalDroid. It has customization options to allow you to create the desired effect.
Edit: Read the documentation on the project's GitHub:
Caldroid fragment includes 4 main parts:
1) Month title view: show the month and year (e.g MARCH, 2013)
2) Navigation arrows: to navigate to next month or previous month
3) Weekday gridview: contains only 1 row and 7 columns. To display "SUN, MON, TUE, WED, THU, FRI, SAT"
4) An infinite view pager that allow user to swipe left/right to change month. This library is taken from https://github.com/antonyt/InfiniteViewPager
Number (1) seems to be what you need.
Personally, for this reasonably simple use case, I'd not bother with some 3rd-party thing you can't easily debug/maintain. I'd use two ViewFlippers -- one for month and one for day. ViewFlipper supports animation, so you can get some nice looking physics to it when the user spins to a month/day.
yes, there is, Calendar Times Square provides to you a calendar to display and then a way to choose from it some date:
first, import the calendar into your project.
then, display it the following way:
//get the current date:
Calendar firstYear = Calendar.getInstance();
Calendar lastYear = Calendar.getInstance();
firstYear.add(Calendar.YEAR, -10);
lastYear.add(Calendar.YEAR, 10);
// to declare the calendar with the current date and make it scrollable for a past year and an upcoming year
final CalendarPickerView calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
calendar.init(firstYear.getTime() , lastYear.getTime())
.withSelectedDate(Calendar.getInstance().getTime());
then, when want to save the selected date:
Date new_date = calendar.getSelectedDate();
Calendar calendar = Calendar.getInstance();
calendar.setTime(new_date);
// to make sure it saved to the system
if (ShellInterface.isSuAvailable()) {
ShellInterface.runCommand("chmod 666 /dev/alarm");
SystemClock.setCurrentTimeMillis(calendar.getTimeInMillis());
ShellInterface.runCommand("chmod 664 /dev/alarm");
}

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

Calling the date time , without initiating the dialog box of Datetime control

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.

Categories

Resources