Android - Hide date field in datepickerdialog

I don't recommend using Reflection to do this kind of thing.

There is a simpler and more pretty way to do so:

((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);

Be aware that .getDatePicker() method from DatePickerDialog works on API LEVEL >= 11.


The source code to DatePicker and DatePickerDialog are available for you to clone, refactor into your own package, and modify to suit.


Private DatePickerDialog createDialogWithoutDateField(){

    DatePickerDialog dpd = new DatePickerDialog(_activity, ExpiryDateSetListener,cyear,cmonth, cday);
    try{
    Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
    for (Field datePickerDialogField : datePickerDialogFields) { 
        if (datePickerDialogField.getName().equals("mDatePicker")) {
            datePickerDialogField.setAccessible(true);
            DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
            Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();
            for (Field datePickerField : datePickerFields) {
               if ("mDayPicker".equals(datePickerField.getName())) {
                  datePickerField.setAccessible(true);
                  Object dayPicker = new Object();
                  dayPicker = datePickerField.get(datePicker);
                  ((View) dayPicker).setVisibility(View.GONE);
               }
            }
         }

      }
    }catch(Exception ex){
    }
  return dpd;

}

Use the above code for hiding the day field in DatePickerDialog.

Refer this LINK