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?
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 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
In my application i have used one datepicker dialog.Which show date picker popup fine and also selcted item displayed correctly.My problem is in call back event it print the date two times so the will be added in db two times when i am insert the date.Please any one help me please any one help me...
DatepickerDialog:
new DatePickerDialog(getActivity(),R.style.Theme_CustomDialog,changeDate,calendarFuture.get(Calendar.YEAR)
,calendarFuture.get(Calendar.MONTH),calendarFuture.get(Calendar.DAY_OF_MONTH)
).show();
Call Back Event is:
DatePickerDialog.OnDateSetListener changeDate=new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calendarFuture.set(Calendar.YEAR, year);
calendarFuture.set(Calendar.MONTH, monthOfYear);
calendarFuture.set(Calendar.DAY_OF_MONTH, dayOfMonth);
Log.i(TAG,"FutureCalendar:"+android.text.format.DateFormat.format("dd MM yyyy", calendarFuture));
}
Output is:
FutureCalendar:13 08 2013
FutureCalendar:13 08 2013
You can try this
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
if(view.isShown())
Log.i(DateDialogFragment.class.getName(),year+"/"+monthOfYear+"/"+dayOfMonth);
}
I want to get last 30 days from the date which I selected from datepicker. Can anyone suggest me the way how can I get that.
I am able to do this functionality using current date but selecting date from datepicker I can not understand how can I do this.
final Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.roll(Calendar.DAY_OF_YEAR, -30);
Date daysBeforeDate = cal.getTime();
I am doing with current date using above code into DatePickerDialog.OnDateSetListener datePickerListener.
Thanks.
You would use code like this for instance:
final Calendar cal = Calendar.getInstance();
DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
cal.roll(Calendar.DAY_OF_YEAR, -30);
}
};
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 ?