I've been searching for a solution to hide any one of the following spinners from the DatePicker. For Android 5.0 the internal variables were changed and, in my case, the Day and Month spinners were visible again after the update to my device.
Android 4.4 and Kitkat Solution
DatePicker dpDate = (DatePicker) findViewById(R.id.dpDate);
// Initialize Date Picker
int year = dpDate.getYear();
int month = dpDate.getMonth();
int day = dpDate.getDayOfMonth();
dpDate.init(year, month, day, this);
Field f[] = dpDate.getClass().getDeclaredFields();
for (Field field : f)
{
// Hides the DAY spinner
if(field.getName().equals("mDayPicker") || field.getName().equals("mDaySpinner"))
{
field.setAccessible(true);
Object dayPicker = new Object();
dayPicker = field.get(dpDate);
((View) dayPicker).setVisibility(View.GONE);
}
// Hides the MONTH spinner
if(field.getName().equals("mMonthPicker") || field.getName().equals("mMonthSpinner"))
{
field.setAccessible(true);
Object monthPicker = new Object();
monthPicker = field.get(dpDate);
((View) monthPicker).setVisibility(View.GONE);
}
// Hides the YEAR spinner
if(field.getName().equals("mYearPicker") || field.getName().equals("mYearSpinner"))
{
field.setAccessible(true);
Object yearPicker = new Object();
yearPicker = field.get(dpDate);
((View) myearPicker).setVisibility(View.GONE);
}
}
Android 5.0+ Lollipop Solution
DatePicker dpDate = (DatePicker) findViewById(R.id.dpDate);
// Initialize Date Picker
int year = dpDate.getYear();
int month = dpDate.getMonth();
int day = dpDate.getDayOfMonth();
dpDate.init(year, month, day, this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
if (daySpinnerId != 0)
{
View daySpinner = dpDate.findViewById(daySpinnerId);
if (daySpinner != null)
{
daySpinner.setVisibility(View.GONE);
}
}
}
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
int monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
if (monthSpinnerId != 0)
{
View monthSpinner = dpDate.findViewById(monthSpinnerId);
if (monthSpinner != null)
{
monthSpinner.setVisibility(View.GONE);
}
}
}
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
int yearSpinnerId = Resources.getSystem().getIdentifier("year", "id", "android");
if (yearSpinnerId != 0)
{
View yearSpinner = dpDate.findViewById(yearSpinnerId);
if (yearSpinner != null)
{
yearSpinner.setVisibility(View.GONE);
}
}
}
I found the above solution to this problem here: Custom Date Picker Dialog in Android Lollipop
I wanted to make sure that it was easily searchable so others could benefit from this so I created this new post. (The solution took me several hours to locate and understand, so I hope to help someone with this).
I've tested this and it works perfectly. In fact, if you put the solution for the Lollipop first and then under that, in the code, put the solution for Kitkat, then it is compatible for both versions without interference. (Make sure to use try/catch ;)
EDIT 7.22.2015: I thought it was obvious if one was using a DatePicker that it needed to be initialized. I included the code to show that you need to initialize the DatePicker before you run the rest of the code (in both situations) otherwise your Views will throw a NullPointerException. That includes the yearSpinner throwing a null. Also, you should have at least one view for the date, don't hide all 3, i.e., don't hide the day, month, and the year at the same time.