I have a DatePickerDialog and I want view only Month and Year. How can I change this code?
public void chooseDate2(View v) {
new DatePickerDialog(
act.this,
d1,
dateAndTime1.get(Calendar.YEAR) + 2,
dateAndTime1.get(Calendar.MONTH),
dateAndTime1.get(Calendar.DAY_OF_MONTH)
).show();
}
private void updateLabel2() {
scadenza.setText(fmtDateAndTime.format(dateAndTime1.getTime()));
}
DatePickerDialog.OnDateSetListener d1=new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateAndTime1.set(Calendar.YEAR, year);
dateAndTime1.set(Calendar.MONTH, monthOfYear);
dateAndTime1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel2();
}
};
Thanks
I don't recommend using Reflection to do this kind of thing.
There is a simpler and more pretty way to do so:
Be aware that
.getDatePicker()
method fromDatePickerDialog
works onAPI LEVEL >= 11
.In addition it does not work on API LEVEL >= 21.
Try the following code. It will show a
DatePicker
with only the year and month (without day)This method returns a date picker dialog. So , in your button's
onClick
method add the following code to display your dialog.More Advance form of Stephan Klein Answer .
MonthYearPickerDialog
And I call it like
month_year_picker.xml
This Works for me fine :
This is an old question, but now you can just use "BetterPickers":
https://github.com/code-troopers/android-betterpickers
And use ExpirationPicker.
Hope this solves it for some other lost souls who searched Google far and wide.
As i recently stumbled across this problem myself, i tested multiple solutions from this post and similar questions on Stackoverflow.
Unfortunately i found no working solution especially for Android 5+
I ended up with implementing my own simple DialogFragment embedding two NumberPickers. This should be compatible with all versions from 3.0 and upwards.
Here is the code:
And the layout
To show the layout use: