4

I'm using the basic DatePickerDialog tutorial by Android found here Android Date Picker

and I've got it working. But I only want to be able to pick a date from this year. So is there a way to remove the year from the Dialog so that only the month and day are displayed? Every time I try and take the Year int out it breaks the code.

kirktoon1882
  • 1,221
  • 5
  • 24
  • 40
  • 1
    Keep in mind that some dates (February 29th!) may or may not be valid depending on the year. –  Jan 05 '13 at 23:29

6 Answers6

9

Yes it possible. DatePicker has the built-in three NumberPicker control, followed by the year, month, day, hide out first to.

final Calendar cal = Calendar.getInstance();
mDialog = new CustomerDatePickerDialog(getContext(), this,
cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
mDialog.show();
DatePicker dp = findDatePicker((ViewGroup) mDialog.getWindow().getDecorView());
if (dp != null) {
    ((ViewGroup) dp.getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
} 

CustomerDatePickerDialog

class CustomerDatePickerDialog extends DatePickerDialog {
     public CustomerDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
         super(context, callBack, year, monthOfYear, dayOfMonth);
     }

     @Override
     public void onDateChanged(DatePicker view, int year, int month, int day) {
         super.onDateChanged(view, year, month, day);
         mDialog.setTitle((month + 1) + "-" + day + "-");
     }
}

DatePicker:

private DatePicker findDatePicker(ViewGroup group) {
        if (group != null) {
            for (int i = 0, j = group.getChildCount(); i < j; i++) {
                View child = group.getChildAt(i);
                if (child instanceof DatePicker) {
                    return (DatePicker) child;
                } else if (child instanceof ViewGroup) {
                    DatePicker result = findDatePicker((ViewGroup) child);
                    if (result != null)
                        return result;
                }
            }
        }
        return null;

    } 
Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • I'm getting an error at new CustomerDatePickerDialog(getContext(), this,.... at getContext(). and changing it to getBaseContext() makes it worse. What would be the fix here? ONe suggestion by Eclipse is to create a getContext() method, but that makes it worse as well. – kirktoon1882 May 04 '12 at 16:52
  • Where do you use 'getContext()' are you in a Activity class? You should pass there the context of the current Activity.. – Cata Jan 05 '13 at 23:28
  • Its not working i have used it but it hides all three spinners and just show datepickerdialog title current OS is 4.1.2 isn`t it due to OS dependency need help and also if i want to remove day rather than year how i need to modify this – Usman Kurd Aug 16 '13 at 07:28
  • mDialog on CustomerDatePickerDialog, is found in ... ? – desgraci Jul 21 '14 at 19:09
  • what is mdialog here? –  Jan 09 '15 at 05:04
9

One liner:

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

Make sure when you build the dialog that you select a leap year (eg. 1904) to ensure February 29 is included in the calendar:

        DatePickerDialog dialog = new DatePickerDialog(getContext(),
                listener,
                1904,
                month,
                dayOfMonth
        );
        dialog.show();
vtlinh
  • 1,427
  • 3
  • 11
  • 16
5

I'm not sure if hide first child it's a good idea. I tested this solution:

  1. Android 2.3 -> it works (day spinner is gone)
  2. Android 4.0 -> it does not work because there is one more element (calendar) and first of the spinners is not a day but month.

Better solution could be modify source code of DatePicker to your own package

Paul
  • 10,381
  • 13
  • 48
  • 86
0

I feel this is a much better implementation. Also, consider duskwuff's comment on the OP. This can make days not appear correctly.

date_picker

<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/datePickerNoYear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

CustomDatePickerDialog.java

public class DatePickerDialogNoYear extends DatePickerDialog {
    public DatePickerDialogNoYear(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
        super(context, callBack, year, monthOfYear, dayOfMonth);
        hideYear();
    }

    public DatePickerDialogNoYear(Context context, int theme, OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) {
        super(context, theme, listener, year, monthOfYear, dayOfMonth);
        hideYear();
    }

    @Override
    public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom) {
        view = findViewById(R.id.datePickerNoYear);
        super.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom);
    }

    private void hideYear() {
        try {
            Field f[] = this.getClass().getDeclaredFields();
            for (Field field : f) {
                if (field.getName().equals("mYearPicker")) {
                    field.setAccessible(true);
                    ((View) field.get(this)).setVisibility(View.GONE);
                }
            }
        } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
            Log.d("ERROR", e.getMessage());
        }
    }
}

Then just use it like any other DatePickerDialog. Good luck!

Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
0

id of year field in datePicker is date_picker_header_year. You can reach datePicker via dialog and hide the year field as follows.

Kotlin:

dialog.datePicker.findViewById<View>(Resources.getSystem().getIdentifier("date_picker_header_year", "id", "android")).visibility = View.GONE
Emre Memil
  • 233
  • 3
  • 5
-1

In this thread you have a very simple solution (more than the previous one), I'm trying it now and works fine.

Hide Year field in Android DatePicker?

Community
  • 1
  • 1
Sergio
  • 1