How to get only month and year picker in Android calendar? - android

I'm using DatePickerDialog with AlertDIalog.THEME_HOLO_LIGHT (in this case I'm getting the desired views) . The problem is , it only works with Android O and above .For < Android O I'm forced to use DatePickerDialog without the AlertDIalog.THEME_HOLO_LIGHT , here I am unable to get a calendar with only month and year.

use this library
Date Picker
then build your date picker like this
new SingleDateAndTimePickerDialog.Builder(this)
.bottomSheet()
.curved()
.displayMinutes(false)
.displayHours(false)
.displayDays(false)
.displayMonth(true)
.displayYears(true)
.displayDaysOfMonth(false)
.display();

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

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.

How to get time time picker from time picker dialog?

On back press in date picker and time picker dialog there is known issue in android jellybean and higher that onDateSet() and onTimeSet() methods are called. To overcome this issue i have found a solution for date picker here(https://stackoverflow.com/a/11493752) but the java file(mentioned in 1st point) provided in it has getDatePicker() method for getting date picker from date picker dialog. But there is no such getTimePicker() method for getting time picker. Can anyone help me with this so that time picker also behaves similar to date picker.
Thanks in advance.
I did a lot of digging and have found a solution to this and it works fine in my project. In time picker we don't need to get time picker from time picker dialog as it does for date picker by getDatePicker() method. Just create a new Time picker and pass that only to listener in onTimeSet().
final TimePicker tp= new TimePicker(getActivity());
Calendar cal=Calendar.getInstance();
final int hour = cal.get(Calendar.HOUR_OF_DAY);
final int minute = cal.get(Calendar.MINUTE);
picker.onTimeChanged(tp, hour, minute);

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.

Fixing a certain year in datepicker in android

I want to fix a certain range of year in datepicker. I only want that range of the year.
I only have the DatePickerDialog object and a OnDateSetListener object, how do I incorporate a datepicker into this arrangement?
Try getting the DatePicker from the dialog using DatePickerDialog.getDatePicker() and then using DatePicker.setMaxDate() and DatePicker.setMinDate()

Categories

Resources