Lollipop CalenderView DatePicker doesn't call OnDateChanged() method - android

I'm working with a DatePicker and finding that under Android 5.0 it will not call the OnDateChanged() method in its OnDateChangedListener when it's in CalendarView mode even though a new date has been selected. If android:datePickerMode="spinner" is set in the DatePicker's xml tag, the DatePicker will appear as spinners and it will call OnDateChanged() when a new date is selected. In earlier versions of Android, a DatePicker calls OnDateChanged() when a new date is selected in both CalendarView and Spinners versions. Here's the relevant code:
#SuppressLint("InflateParams")
View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_date, null);
DatePicker datePicker = (DatePicker) v.findViewById(R.id.dialog_date_DatePicker);
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
//Translate year, month, day into a Date object using a calendar
mDate = new GregorianCalendar(year, month, day).getTime();
//Update argument to preserve selected value on rotation
getArguments().putSerializable(EXTRA_DATE, mDate);
}
});
In my application, onDateChanged() doesn't get called and mDate doesn't get changed if the DatePicker is in CalendarView mode under Lollipop, but OnDateChanged() does get called and mDate does change if the DatePicker is in Spinners mode. Under earlier versions of Android, OnDateChanged() gets called and mDate gets changed in both versions of the DatePicker.
Is there any way to get a CalendarView DatePicker in 5.0 to call OnDateChanged()? Failing that, how else can I retrieve a changed date from the DatePicker when it's in CalendarView mode?

I face the same issue and the thing is onDateChange() and onTimeSet() listeners for DatePicker and TimePicker is not called in Nexus devices with lollipop Update.
The reason is in nexus devices, since the clock app is updated, the listeners are not working.
The work around is once the dialog dismiss, you need to create you own listener and set the values in a calendar object using the datepicker get() methods and pass the calendar to the listener.
A simple sample code is
/**
* Returns the calendar instance once the date and time is set.
* #return
*/
public Calendar getDateTime() {
mCalendar.set(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
return mCalendar;
}

Set this in your Xml-Layout:
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:spinnersShown="false"
android:id="#+id/datepicker_popupwindow"/>
With "android:spinnersShown="false" " you tell it to not show the Spinner. But it will call the 'onDateChanged' Method.

Related

How to set the limit on date in Date picker dialog

I want to put the limit on date so that user can not pick date more then that, for example if today is 1 January then User should not be able to select more then 7 dates , I mean he can not select 9 January. I also want him not to select the month and year. So I am putting a limit to set his task in one week.
what I have done so far is showing the date picker fragment and setting current date in it. the code in my main activity goes like this:
etSelectDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DialogFragment datePickerFragment = new DatePickerFragment() {
#Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Log.d("Date Change", "onDateSet");
Calendar c = Calendar.getInstance();
c.set(year, month, day);
DateFormat df = DateFormat.getDateInstance();
etSelectDate.setText(df.format(c.getTime()));
//nextField.requestFocus(); //moves the focus to something else after dialog is closed
}
};
datePickerFragment.show(MainActivity.this.getSupportFragmentManager(), "datePicker");
}
});
and date picker fragment class goes like this :
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
#Override
public void onDateSet(DatePicker view, int year, int month, int day) {
//blah
}
}
till then its is working fine , but I do not know how to put the limit on date and rest of the months and year should be non Select able . I have seen many link such as like this , but I do not understand How can I do that and also there is nothing helpful on android site.
So please help me , How can I put limit of seven days only
Update
Through your replies I know how to set the max date in calender , so As I want to set the max date 7 days ahead of current date , I am still not getting it. the method I read so far is :
pickerDialog.getDatePicker().setMaxDate(new Date().getTime());
It is setting the current date as maximum, but How can I add 7 days ahead in it since it is Date object ? please help
You have the setMinDate(long) and setMaxDate(long) methods at your disposal. Both of these will work on API level 11 and above. Since you are using a DatePickerDialog, you need to first get the underlying DatePicker by calling the getDatePicker() method.
dpdialog.getDatePicker().setMinDate(minDate);
dpdialog.getDatePicker().setmaxDate(maxDate);
Source :Set Limit on the DatePickerDialog in Android?
You can calculate the minDate by using,
Date today = new Date();
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add( Calendar.MONTH, -6 ) // Subtract 6 months
long minDate = c.getTime().getTime() // Twice!
Updated :
Replace the below line
return new DatePickerDialog(getActivity(), this, year, month, day);
with
// Create a new instance of DatePickerDialog and return it
DatePickerDialog pickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
pickerDialog.getDatePicker().setMaxDate(maxDate);
pickerDialog.getDatePicker().setMinDate(minDate);
return pickerDialog;
In some cases, when you set maximum date with a date without hours and minutes, you won't be able to select the maximum date you set.
For instance
Calendar maxDate = Calendar.getInstance();
maxDate.set(Calendar.DAY_OF_MONTH, day + 5);
maxDate.set(Calendar.MONTH, month);
maxDate.set(Calendar.YEAR, year);
datePickerDialog.getDatePicker().setMaxDate(maxDate.getTimeInMillis());
With the code block above, you can not click to your maxDate.
But if you add hours and minutes to like;
maxDate.set(Calendar.HOUR, 23);
maxDate.set(Calendar.MINUTE, 59);
to your maxDate, your last date will be clickable
by using
setMinDate and setMaxDate
MinDate
MaxDate
try this
DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();
Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMaxDate(calendar.getTimeInMillis());//set the current day as the max date or put yore date in miliseconds.
datePicker.setMinDate(calendar.getTimeInMillis());//set the current day as the min date or put your date in mili seconds format
return dpDialog;

Bug with setting max date on DatePickerDialog

I have created a datepicker and set the max date to the current date but I am getting a weird bug. It shows the next month and next day below the wheel when nothing should be there. When you try to scroll to them they disappear and are not selectable.
my datepicker code:
DatePickerDialog dialog = new DatePickerDialog(this, datePickerListener, year, month, day);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;
how im getting year, month, day:
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
Here is a picture of what is happening
I had the same problem.
My solution was to set also the hour of the Calendar object to 11:
yourCalendarObject.set(Calendar.HOUR, 11);
And then use the calendar object to set the setMaxDate from your Datepicker.
yourDatePicker.setMaxDate(yourCalendarObject.getTimeInMillis());
Hope it helps anyone.
EDIT: I highly recommend to use a library for all this.
It takes care of the complete handling with the datpickers and also solved my bug:
https://github.com/wdullaer/MaterialDateTimePicker

Manual input not saved in Android's DatePicker(Dialog)

Implementing the DatePicker or DatePickerDialog in Android is easy. But when it comes to data storage, I have a problem with those classes:
If you use the spinners (+ or - button) to change the date, everything works fine. The event "Date changed" or "Date set" is called and you can get the values that the user entered.
But when the year is manually entered into the input field (via keyboard) and the user then clicks "Save" in the dialog, there won't be any event called and you won't get that manually entered value.
It only works when the user changes something with the sliders again after manually entering the year. Because when you use the sliders, the events are fired.
Is this normal behaviour? How can I achieve the desired behaviour, namely that an event is fired when the user enteres something manually and then clicks "Save"?
Thanks in advance!
Just clear focus, and android will set the number from manual input.
eg:
DatePicker datePicker = findViewById(R.id.dp);
When saving just like onClick(), add
datePicker.clearFocus();
This must be working.
I had the same problem and the accepted answer really helped me a lot. My situation is a little different though as I'm using DatePickerDialog. Here's how the DatePicker properly worked finally.
Declare the variables first and then define them.
private DatePickerDialog.OnDateSetListener date;
private DatePickerDialog mDatePickerDialog;
private Calendar myCalendar;
// Get the calendar instance
myCalendar = Calendar.getInstance();
// Define the date set listener first.
date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// Do something with year, monthOfYear and dayOfMonth
}
};
// Now define the DatePickerDialog
mDatePickerDialog = new DatePickerDialog(context, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
mDatePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Set", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
DatePicker datePicker = mDatePickerDialog.getDatePicker();
// The following clear focus did the trick of saving the date while the date is put manually by the edit text.
datePicker.clearFocus();
date.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
}
});
Then inside an onClick of a button
mDatePickerDialog.show();
If you look at the Date Picker Example, the DatePickerDialog.OnDateSetListener callback is received as soon as the user clicks the 'SET' button on the dialog.
Look at the dialog below
Even if you enter the date using the keyboard, the date itself is not accepted until you click the 'SET" button in the dialog and that is when the DatePickerDialog.OnDateSetListener callback is called.

How to stop the Date Picker Dialogue-box Date to set the previous Dates. It should allow only above System current date date

Hi every one thanks in advance..
I have requirement in my App that I have a Date Picker to set the date. Now I want to restrict the Date Picker to set the date above System current dates but not below to the System current date. How....?
Can any one help me out from this.... And How to validate the System current date with the Date Picker Date.
In this case you can extend DatePickerDialog and make your own implementation of OnDateChanged, which is called everytime that the date changes and you get as parameters the DatePicker, and the new year, month and day values, so you can check if that date is past and in that case throw the error (with a Toast or whatever) and call DatePicker.updateDate() to set a correct value (so that DatePicker is allways in a consistent state).
Also, you can call to DatePicker.init(year, monthOfYear, dayOfMonth, onDateChangedListener); then you can pass a
onDateChangedListener implementation without having to extend DatePickerDialog.
EDIT: (I never try this but I think it can done your work..)
DatePicker's
setMinDate(long minDate)
Sets the minimal date supported by this NumberPicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
Example:
DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
dialog.getDatePicker().setMinDate(new Date());
Use setMinDate function of CalendarView class. Here you can set your date in milli seconds. To prevent future dates use setMaxDate
Use getDatePicker to get the datepicker and set calendarView as told above.
private int myear; //Declare these three variables in MainActivity and call showDialog(1)
private int mmonth;
private int mday;
final Calendar myCalendar= Calendar.getInstance();
private void setCurrentDateOnView() { //Call this method before showDialog
myear = myCalendar.get(Calendar.YEAR);
mmonth = myCalendar.get(Calendar.MONTH);
mday = myCalendar.get(Calendar.DAY_OF_MONTH);
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
DatePickerDialog startDate = new DatePickerDialog(this, datePickerListener, myear,mmonth,
mday){
#Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
}
};
myCalendar.set(myear, mmonth, mday);
long startTime = myCalendar.getTimeInMillis();
startDate.getDatePicker().setMinDate(startTime - 1000);
return startDate;
}
return null;
}

Dialog datePicker bounds - Android

I'm using a datePicker dialog to pick the date, and I must set a lower and an upper bound. How can i do this?
Should I use the public void onDateChanged(DatePicker view, int year, int month, int day) method?
Please be explicit because I'm working with Android from 4 days only!
I suppose a DatePicker can NOT have a bound range. But you can check the date picked by user yourself. And if the date is invalid, you can alert the user.
You can set the maxDate and minDate like this:
Date upperLimit = new Date();
Date lowerLimit = new Date();
DatePicker picker = new DatePicker(getContext());
picker.setMaxDate(upperLimit.getTime());
picker.setMinDate(lowerLimit.getTime());
Cheers

Categories

Resources