Skip to content Skip to sidebar Skip to footer

Hide Year Field In Android Datepicker?

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 under

Solution 1:

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());
    }

Solution 2:

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...

Solution 3:

Easy way to disable Year field in date picker

DatePickerdp= 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;
            } elseif (child instanceof ViewGroup) {
                DatePicker result = findDatePicker((ViewGroup) child);
                if (result != null)
                    return result;
            }
        }
    }
    returnnull;

}

I have refered below link for reference Remove year from DatePickerDialog

Solution 4:

You can set range of the year by using this function

datePicker.setYearRange(1950,2017);

Solution 5:

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.

Post a Comment for "Hide Year Field In Android Datepicker?"