14

I'm using a basic DatePicker and I'm trying to figure out how to hide the Year field on the DatePickerDialog so that only the Month and Day are visible. I don't mind that the underlying code for the year would still be there, I'd just like to hide the Year Field in the Dialog. You know with something like:

((View) myYear).setVisibility(View.GONE);

which I know doesn't work because myYear is a int not a View, but something along those lines. Is it possible?

kirktoon1882
  • 1,221
  • 5
  • 24
  • 40

5 Answers5

21

A super easy way that I found to implement a DatePicker is to call it in xml:

    <DatePicker
    android:id="@+id/thePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

and then hide whichever field you want (in this case the year) in java:

    picker = (DatePicker) findViewById(R.id.thePicker);
    try {
        Field f[] = picker.getClass().getDeclaredFields();
        for (Field field : f) {
            if (field.getName().equals("mYearPicker")) {
                field.setAccessible(true);
                Object yearPicker = new Object();
                yearPicker = field.get(picker);
                ((View) yearPicker).setVisibility(View.GONE);
            }
        }
    } 
    catch (SecurityException e) {
        Log.d("ERROR", e.getMessage());
    } 
    catch (IllegalArgumentException e) {
        Log.d("ERROR", e.getMessage());
    } 
    catch (IllegalAccessException e) {
        Log.d("ERROR", e.getMessage());
    }
my code smells
  • 461
  • 5
  • 14
kirktoon1882
  • 1,221
  • 5
  • 24
  • 40
  • 12
    You can replace `if (field.getName().equals("mYearPicker")) {` with `if (field.getName().equals("mYearPicker") || field.getName().equals("mYearSpinner")) {` to account for DatePicker widget in Honeycomb and higher versions of Android... – Hidden Android Dec 12 '12 at 08:50
  • 2
    for latest Androids, also hide "mCalendarView" – YKS Oct 07 '14 at 12:10
9

Be really careful with this solution. you are using reflexion to access a field and change the visibility. This will not work with Android 5.0 ! Google have changed the implementation of the TimePicker and DatePicker and the classes are no longer exposing any fields appart from the delegate.

I don't know the solution yet for Android 5.0+.I will let you know as soon I found something interesting.

As an idea, you could use the Build.VERSION.SDK_INT to check the Android version and then try to access the DatePickerSpinnerDelegate using reflexion and set the field you want.

Again, this is not the ideal solution as Google are free to change this again and again...

rontho
  • 439
  • 4
  • 11
  • so what is the solution for android 5.0+ ? – RDD Sep 10 '15 at 05:37
  • I never found any good solution. For our project I have simply used the provided DatePicker with the year selection... You can also have a look at this https://android-arsenal.com/tag/27. It might be useful if you are looking for a customizable DatePicker library. – rontho Sep 11 '15 at 07:09
  • 1
    i created a custom component by following [this](http://stackoverflow.com/questions/21321789/android-datepicker-change-to-only-month-and-year) and got what i want. – RDD Sep 14 '15 at 05:51
1

Easy way to disable Year field in date picker

DatePicker dp = findDatePicker((ViewGroup) dialog.getWindow().getDecorView());

if(dp != null) 
{
    ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setEnabled(false);
    ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setAlpha(0.4f);
}

if you want to remove year field just setvisibility of result view to View.GONE like below

dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE)



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;

}

I have refered below link for reference Remove year from DatePickerDialog

Community
  • 1
  • 1
  • crashes on Android 7 - Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference – Andrey Rankov Aug 08 '17 at 09:48
-1

Recently managed to hide the year part of the dialog by setting the datepicker width too small.

<DatePicker
        android:id="@+id/month_day_datepicker"
        android:layout_width="140dp"
        android:datePickerMode="spinner"
        style="?android:attr/borderlessButtonStyle"/>

Let me know if this works, obviously, there is an accessible year in the binding but you can just not pick that up when calculating the date.

Yonz
  • 120
  • 6
-3

You can set range of the year by using this function

datePicker.setYearRange(1950,2017);
शु-Bham
  • 952
  • 1
  • 7
  • 14