I want a date picker to show only Month and Year. I've customized the Date Picker to do so i.e., to remove 'day' field from the picker,but in Android Lollipop Am getting picker with Day, Month and Year. Following is my piece of code. Please help me to know the problem. Thanks in advance.
try {
Field f[] = mDatePicker.getClass().getDeclaredFields();
for (Field field : f) {
if (field.getName().equals("mDaySpinner") || field.getName().equals("mDayPicker")) {
field.setAccessible(true);
Object dayPicker = new Object();
dayPicker = field.get(mDatePicker);
((View) dayPicker).setVisibility(View.GONE);
}
}
} catch (SecurityException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
Use this sample project Android month and year picker
https://github.com/developersuru/android-month-year-picker
Using reflection to find and hide UI elements is not really a great practice. In your case, it stopped working in lollipop because the
mDaySpinner
is now contained in an internal private staticDatePickerSpinngerDelegate
class within theDatePicker
class.I would recommend going through the view hierarchy to find and hide the day spinner element instead. I wrote the following code that works in lollipop:
This is a basic example of a month picker, but can be easily adapted to pick also year and/or day (works also for older android versions):
}
Went ahead making a modified Date Picker called Simple Date picker ... have used the code similar to Date Picker just to show month and year
see https://github.com/resengupta/Month-Year-Date-Picker
SimpleDatePickerDialog.java class is responsible for showing the month and year number picker. SimpleDatePickerDelegate.java works to apply rules to the number pickers. SimpleDatePickerDialogFragment.java is a DialogFragment which wraps the alert dialog.