I have implemented DatePickerDialog in my app and I am having the following issues with Lollipop only.
Code Snippet:
void setDate(final EditText ed) {
Calendar mcurrentTime = Calendar.getInstance();
int day = mcurrentTime.get(Calendar.DAY_OF_MONTH);
int month = mcurrentTime.get(Calendar.MONTH);
int year = mcurrentTime.get(Calendar.YEAR);
DatePickerDialog mTimePicker;
mTimePicker = new DatePickerDialog(getContext(), new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int selectedYear, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
monthOfYear += 1;
ed.setText(selectedYear + "-" + monthOfYear + "-" + dayOfMonth);
}
}, year, month, day);
//mTimePicker.setTitle("Select Date:");
mTimePicker.show();
}
I am not able to select date of last week and on pressing next it's displaying next month.
Has anyone run into a problem like this? Does anyone have a solution?
Any help is appreciated, thank you.
I'm seeing the same issue as well.
It has nothing to do with min or max dates set on the datepicker...
It seems to be an issue arising from the screen size being small and thus the datepicker cuts off the last week of the month(or more if the screen in really small) and the 'days' area is not scrollable(up/down)... scrolling left/right will take you to the previous/next month...
My idea for a fix would be to make the 'days' area of the datepicker vertically scrollable but as this is a default android widget I am not sure how to go about doing this -_-
P.S. I know this is not an answer but my rep prevents me from making a comment thus this is the only option open to me...
You can set Min and Max date to your calendar here is a code snippet below
Calendar calendar = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar calendar1 = Calendar.getInstance();
calendar1.set(year, monthOfYear, dayOfMonth);
Date date = new Date(calendar1.getTimeInMillis());
if (date.after(new Date(Calendar.getInstance().getTimeInMillis()))) {
Toast.makeText(ProfileFirstFragment.this.getActivity(),
getString(R.string.error_wrong_date_selected), Toast.LENGTH_SHORT).show();
return;
}
dob.setText(getString(R.string.date_slash, new String[]{String.valueOf(dayOfMonth),
String.valueOf(monthOfYear + 1), String.valueOf(year)}));
}
},
1990, 0, 1); //this can be your calendar startdate when you open the calendar
calendar.set(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH),
Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().getMaximum(Calendar.HOUR_OF_DAY),
Calendar.getInstance().getMaximum(Calendar.MINUTE), Calendar.getInstance().getMaximum(Calendar.SECOND));
datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());// this will be your max date you can select
calendar.add(Calendar.YEAR, -90);
datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
datePickerDialog.show();//this will be minimum date in your calendar
I hope this helps !!
While creating DatePickerDialog instance, Pass the minimum date you want to alllow user.
new DatePickerDialog(getContext(), new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int selectedYear,
int monthOfYear, int dayOfMonth) {
// TODO
}
}, year, month, 1); // for current month's first day as minimum
and in next line, before show call method updateDate
mDatePicker.updateDate(year,month,day); // for current date to set selected
Related
One of my features require the user to select a month and year, so the following is the method which will pop up the first attached image (d/m/y).
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);
DatePickerDialog dp = new DatePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_MinWidth,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//Check if selected month/year is in future of today.
}
}, year, month, day);
dp.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dp.setTitle("Set Monthly Overview");
dp.show();
However, i do NOT require the date. How can i disable user from selecting it, or best of all, to even not display the date option there?
What I wish to achieve is something like the following image in the best case scenario.
Refer "Android DatePicker change to only Month and Year" which will help you just display month and year in the datepicker. Hope this helps.
I'm trying to prevent the user from selecting a future date on a DatePicker.
As I'm targeting at least Android 2.3 I cannot make use of setMaxDate. So I tried to develope a little workaround for this:
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth){
Calendar selectedDate = Calendar.getInstance();
selectedDate.set(year, monthOfYear, dayOfMonth);
Calendar now = Calendar.getInstance();
if (selectedDate.after(now)){
datePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE));
}
}
But this produces a StackOverflowError - maybe because it's onDateChanged and not afterDateChanged and the method therefore calls itself infinitely??
But what could I do instead?
I had a date picker I want to set a min date so that when a user is setting up a date It should not allow bellow that min date and that date should be not less than tomorrow
I had done
//==============Date Picker============================
#Override
protected Dialog onCreateDialog(int id) {
switch(id){
case ID_DATEPICKER:
return new DatePickerDialog(this,myDateSetListener,myYear, myMonth, myDay);
default:
return null;
}
}
private DatePickerDialog.OnDateSetListener myDateSetListener = new DatePickerDialog.OnDateSetListener()
{
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
String date = String.valueOf(dayOfMonth)+"/"+ String.valueOf(monthOfYear+1) +"/"+ String.valueOf(year);
dateEditText.setText(date);
}
};
//==============Date Picker============================
Use setMinDate (long minDate) to set the minimal date supported by this NumberPicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
If you are using DatePickerDialog use getDatePicker () to get DatePicker
Set an OnDateChangedListener to your DateTimePicker, where in
onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
check if the date is in your allowed range and if not, call view.updateDate with maximum or minimum allowed (for example whichever is closest to the entered date).
I placed a datepicker on a layout to let the user select their birthday. I have used datepickerdialogs earlier by clicking on a button and bringing the dialog up. But this time the datepicker itself is on the layout and I can select a date. How can I get the date every time the user changes the day, month or the year?
Solution:
dpResult = (DatePicker) findViewById(R.id.dpResult);
dpResult.init(year, month, day, new OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
//here I got all the values I need
}
});
I guess you have to use init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener yourListenerHere). Did you try this ?
in my application i have to date picker as From date and To date. I am using onCreateDialog() method. So first time i am selecting the dates from date picker and putting them in the edit text. then when again i am selecting the date from the date picker, in the date picker it is showing the date which i already selected. But if i move to other search in the same screen(Activity) and try to pick the date from date picker, date picker still is showing the previous date. not the current date.
protected Dialog onCreateDialog(int id) {
final Calendar c = Calendar.getInstance();
switch (id) {
case DATE_DIALOG_ID1:
return new DatePickerDialog(this, mDateSetListener, c.get(Calendar.YEAR),
c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
case DATE_DIALOG_ID2:
return new DatePickerDialog(this, mDateSetListener2, c.get(Calendar.YEAR),
c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
}
return null;
}
May be you can try writing your question clearly once again, but to my understanding of your question you might want to look at your onDateChanged() callback method. I assume you have a updateDialog() method inside this onDateChanged(), you might want to check that method for err's.
I suppose you looked this link where they showed how to use a date picker http://developer.android.com/resources/tutorials/views/hello-datepicker.html
good luck
For the 2nd question you asked about mixup in from and to date dialog
From what you are saying, I think there something like a small error like
1) R.id.datepicker2 being used in place of R.id.datepicker1
or
2) having mixs up in onDateChangedListener(). I am sure there is better way to do it, but try checking whether you have like this below 2 different listeners
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePicker fromDatePicker = (DatePicker) findViewById(R.id.datePicker1);
datePicker.init(mYear, mMonth, mDay, new OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//things you want to happen when datepicker1 is changed in your case the From date field
}
});
DatePicker toDatePicker = (DatePicker) findViewById(R.id.datePicker1);
datePicker.init(mYear, mMonth, mDay, new OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//things you want to happen when datepicker2 is changed in your case the To date field
}
});