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?
Solution 1:
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:
or
That's the fancy way. The ugly way, you could do this:
Android Version < 3.0
package my.pkg;
...
classMyDatePickerextendsDatePicker {
publicMyDatePicker(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)
publicMyDatePicker(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"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><my.pkg.MyDatePickerandroid:layout_width="fill_parent"android:layout_height="wrap_content" /></LinearLayout>
Post a Comment for "Android Datepicker Month As Integer"