Sunday, March 13, 2016

DatePicker, Sony Xperia and dates before 1980

I had a user report that they could not set a birth date before 1980 on their Sony Xperia phone. Bit of a problem if you are older than 36.

To fix this, first add the following to where ever you are inflating your datepicker:

this.datePicker = (DatePicker) view.findViewById(R.id.datePicker);
// hack also added to styleif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    Calendar calendar1 = Calendar.getInstance();
    calendar1.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
    datePicker.setMinDate(calendar1.getTimeInMillis());
}


Unfortunately, the DatePicker.setMinDate is not exposed pre HONEYCOMB so to fix those apps, you can try adding this to your base application style

<!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->    <item name="android:startYear">1900</item>
</style>

No comments:

Post a Comment