kirktoon1882
kirktoon1882

Reputation: 1231

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

Upvotes: 14

Views: 21891

Answers (5)

Yonz
Yonz

Reputation: 180

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.

Upvotes: 0

शु-Bham
शु-Bham

Reputation: 952

You can set range of the year by using this function

datePicker.setYearRange(1950,2017);

Upvotes: -3

Suresh Vemuri
Suresh Vemuri

Reputation: 37

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

Upvotes: 2

rontho
rontho

Reputation: 439

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

Upvotes: 9

kirktoon1882
kirktoon1882

Reputation: 1231

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

Upvotes: 21

Related Questions