carbonr
carbonr

Reputation: 6067

How to display date picker for android with only month and year fields?

i want to disable the day selection option on the android sdk date picker. any easy xml configuration would be the best

Upvotes: 21

Views: 44913

Answers (8)

Mayuresh Deshmukh
Mayuresh Deshmukh

Reputation: 1127

For Hiding Day :

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

&

For hiding Month :

datePicker.findViewById(Resources.getSystem().getIdentifier("month", "id", "android")).setVisibility(View.GONE);

Upvotes: 0

Bogdan Aksonenko
Bogdan Aksonenko

Reputation: 460

I think It's the best solution

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

Upvotes: 2

Daniel
Daniel

Reputation: 2662

I just have released a new date picker framework which allows you to create custom date picker. I also provided some example date pickers like the one you are looking for. Hope, that it works for you.

the code can be found here: https://github.com/bendemboski/DateSlider

UPDATE 06/2014: This library was developed 2010 and has been unmaintained since 2011. So it is most likely out of date by now.

Upvotes: 16

blyabtroi
blyabtroi

Reputation: 2076

Speaking of reflection this works in Android 4.4.2 SDK

"mDaySpinner"

instead

"mDayPicker"

Upvotes: 3

mspapant
mspapant

Reputation: 2050

You can use the https://github.com/SimonVT/android-datepicker widget for backported compatibility and make the day picker 'gone'

<net.simonvt.numberpicker.NumberPicker
        android:id="@+id/day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dip"
        android:layout_marginRight="16dip"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:visibility="gone"
        />

Upvotes: 0

Nils
Nils

Reputation: 5729

EDIT: as per the comment below, I wouldn't follow this answer anymore.

I don't think you can do that with the default date picker. You'll have to create it out of basic Android UI elements. But instead of going to that trouble, why not use a better library element like this great-looking iPhone-esque wheel: http://code.google.com/p/android-wheel/ I haven't used it yet, but I plan to!

Upvotes: 1

Luis E. Fernandez
Luis E. Fernandez

Reputation: 846

This works fine in 2.2 version. But title of the dialog is not changed.

for (Field datePickerDialogField : datePickerDialogFields) {           

    if (datePickerDialogField.getName().equals("mDatePicker")) {
        datePickerDialogField.setAccessible(true);
        DatePicker datePicker = (DatePicker) datePickerDialogField.get(datePickerDialog);

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

Upvotes: 0

augustorsouza
augustorsouza

Reputation: 151

It possible to hack the DatePicker instance using reflection. This way, you are able to access the NumberPicker instance which represent the day in the DatePicker:

datePicker = (DatePicker) findViewById(R.id.expiration_date);
try {
    Field f[] = datePicker.getClass().getDeclaredFields();
    for (Field field : f) {
        if (field.getName().equals("mDayPicker")) {
            field.setAccessible(true);
            Object dayPicker = new Object();
            dayPicker = field.get(datePicker);
            ((View) dayPicker).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: 15

Related Questions