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.
Related
I'm experiencing a weird visual bug on new DatePickerDialog of Android widgets. I need to set a dialog's selectable date range as 1 month but it shouldn't also select a date from the future. Logic works as expected but the visuals have some sort of bug. Here is how it looks:
Note that the "5" is disabled, I cannot select it. However, it looks as selectable. What should I do to solve this issue? Any help is appreciated, thanks. (Today's day of month is also 5, I assume this bug comes from there.)
Edit: This is how I create the dialog.
#SuppressLint("ValidFragment")
public static class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
private static final long MONTH = 2592000000L;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Use previous selected date in the date picker
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(DateUtils.getAtTimeTimestamp(startDate));
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), R.style.AppTheme_DialogTheme, this, year, month, day);
Date date = new Date(); // get current time
// get target maximum date which can be 1 month above
long targetDateTime = DateUtils.getAtTimeTimestamp(startDate) + MONTH;
if (date.getTime() < targetDateTime)
dialog.getDatePicker().setMaxDate(date.getTime()); // now because it can't be future
else
dialog.getDatePicker().setMaxDate(targetDateTime); // should also be one month above from the start date.
dialog.getDatePicker().setMinDate(DateUtils.getAtTimeTimestamp(startDate));
return dialog;
}
#Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2)
{
// logic between date selections
}
}
DateUtils.getAtTimeTimestamp(String date) basically converts a string to long integer that is time in milliseconds.
Updates:
DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// do some stuff
}
}, year, month, day);
DatePicker datePicker = datePickerDialog.getDatePicker();
datePicker.updateDate(2018, 2, 7);
datePicker.setMinDate(1272920400000L); //milliseconds since epoch for 2010.04.10
datePickerDialog.show();
The problem happens inside any DatePicker object, it's not because of my code. You just go inside a DatePickerDialog and try to replicate.
I have a DatePicker and I've set minDate to be 4th of May 2010.
But when you select, for example, 1st May 2011, and then click left arrow until you reach May 2010, it will be automatically selecte 1st May 2010, which is unavailable. Then, OK button will work.
What do you think about this? Is there a way to solve it? I've searched all the Internet and didn't find anything. Thank you!
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
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
}
});