marbarfa
marbarfa

Reputation: 677

Android DatePicker month as integer

I have a simple question but after searching a while I couldn't find the answer yet. In a DatePicker, is it possible to change the month to be displayed as an integer not a string?.eg: {1,2,3..} not {jan,feb,mar...}

EDIT:

Although @kaneda's solution seems to work, in android 3.0 at least the methods and attributes are not the same. The code I'm using is this:

public DatePickerCustom(Context context, AttributeSet attrs) {
       super(context, attrs);
       Field[] fields = DatePicker.class.getDeclaredFields();
       try {
           for (Field field : fields) {
               field.setAccessible(true);
               if (TextUtils.equals(field.getName(), "mMonthSpinner")) {
                   Method m =     field.getType().getDeclaredMethod("setDisplayedValues", String[].class);
                   m.setAccessible(true);
                   String[] s = new String[]     {"01","02","03","04","05","06","07","08","09","10","11","12"};
                   Object[] params = new Object[1];
                   params[0] = s;
                   m.invoke(field.get(this), params);
                   break;
               }
           }
       }
       catch (Exception e) {
           System.out.println(e.getMessage());
           e.printStackTrace();
       }
    }

EDIT 2:

Best solution, is to create a Custom DatePicker (following @kaneda first advice), will always work without compatibility issues and not relaying on reflection.

Used the date_picker.xml from android and DatePicker.java posted by @kaneda. Also, I used a custom NumberPicker (because the one the DatePicker widget uses is internal to android). For the NumberPicker widget I followed this link.

In the CustomDatePicker I commented the function getShortMonths() and set the month range as mMonthPicker.setRange(1, NUMBER_OF_MONTHS);.

Upvotes: 1

Views: 4285

Answers (1)

kaneda
kaneda

Reputation: 6189

For what concerns best practices I think it's the most correct and easier for you if you create your own custom DatePicker by creating your own layout, so I suggest you take a look at its source code just for reference:

Android Source Code

or

Here

That's the fancy way. The ugly way, you could do this:

Android Version < 3.0

package my.pkg;
...
class MyDatePicker extends DatePicker {

    public MyDatePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        Field[] fields = DatePicker.class.getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                if (TextUtils.equals(field.getName(), "mMonthPicker")) {
                    Method m = field.getType().getDeclaredMethod("setRange", int.class, int.class, String[].class);
                    m.setAccessible(true);
                    String[] s = new String[] {"01","02","03","04","05","06","07","08","09","10","11","12"};
                    m.invoke(field.get(this), 1, 12, s);
                    break;
                }
            }
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

}

Android Version >= 3.0 (only the constructor)

public MyDatePicker(Context context, AttributeSet attrs) {
    super(context, attrs);
    Field[] fields = DatePicker.class.getDeclaredFields();
    try {
        String[] s = new String[] {"01","02","03","04","05","06","07","08","09","10","11","12"};
        for (Field field : fields) {
            field.setAccessible(true);
            if (TextUtils.equals(field.getName(), "mMonthSpinner")) {
                NumberPicker monthPicker = (NumberPicker) field.get(this);
                monthPicker.setDisplayedValues(s);
            } 
            if (TextUtils.equals(field.getName(), "mShortMonths")) {
                field.set(this, s);
            }
        }
    }
    catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}

Then the boilerplate xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <my.pkg.MyDatePicker
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

enter image description here

Upvotes: 3

Related Questions