DatePickerDialog display year picker first - android

I want my DatePickerDialog as soon as it opens to display the list of years.
For example, when I show the dialog I see the calendar view:
:
If I tap on the text "1900" the year view is shown:
I tried this code but it had no effect and I'm clueless for what I could do:
DatePickerDialog birthDatePicker = new DatePickerDialog(getActivity(), this, 0, 0, 0);
birthDatePicker.getDatePicker().setSpinnersShown(true);
birthDatePicker.show();

Same as Show year selection first in Android Calendar View
I will copy here my answer:
You can get a reference to year view and then just simulate user click to open it.
dataPicker.getTouchables().get( 0 ).performClick();

Using the Datepicker XML modifications you can elect to show spinners instead of the Calendar, which might be beneficial for you as you want to directly edit the years.
android:datePickerMode="spinner"

Related

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

Date is shown twice if setMaxDate is used on DatePickerDialog

When setting the maximum date on a date picker dialog like this:
DatePickerDialog dialog = new DatePickerDialog(this, listener, year, month, day);
dialog.getDatePicker().setMaxDate(new Date().getTime());
The date is shown normally, but also a second time at the top of the dialog. I've seen that many people have encountered this bug but I didn't find a solution.
You can also see it on this image from another user
Is there any way to prevent or solve this, besides using reflection?
I solved the bug by calling dialog.setTitle(null) every time a maximum date is set.
I posted this issue here: https://issuetracker.google.com/72284741

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

Not to show the date before Present date Android DatePickerDialog

Is there a way to not to show the dates/months before present date instead of disabling the dates in Android.
Totally agree with above answer :-
Calendar c = Calendar.getInstance();
c.set(2000,01,01);
datePicker.setMinDate(c.getTime().getTime());
Using above lines datepickerdialog is not showing prevous date anymore with your datepicker dialog.

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.

Categories

Resources