Reputation: 10641
I am experiencing what I believe is a DatePicker bug.
I have a date picker dialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp"
>
<DatePicker
android:id="@+id/datePicker"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:calendarViewShown="false"
android:layout_gravity="center_horizontal"
android:paddingBottom="20dp"
android:paddingTop="10dp"
>
</DatePicker>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="1"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/cmdCancel"
android:text="@android:string/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
>
</Button>
<Button
android:id="@+id/cmdOK"
android:text="@android:string/ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
>
</Button>
</LinearLayout>
</LinearLayout>
I get a handle to it and display the dialog when a date entry is required. The Ok
button code is such:
Button cmdOK = (Button)dialogDate.findViewById(R.id.cmdOK);
cmdOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth()+1; // offset index
int year = datePicker.getYear();
String dob = month+"/"+day+"/"+year;
txtDOB.setText(dob);
closeDialogDate();
} catch (Exception e) {
e.printStackTrace();
}
}
});
Everything works as expected sans one scenario and I can repeat the behavior.
The issue:
If I select the year by tapping on the value and get a number keypad, enter the date and it's the LAST THING ENTERED, whether I hit done
on the keypad or just hit the Ok
button, executing the processing, the year does not get updated correctly. If I select day or month values before hitting Ok
the year is correctly updated. If I use the spinners, the year is updated correctly. The only time the bug appears is when it's the LAST and directly entered value.
What am I not doing correctly?
This is NOT a -1900 issue. With the provided screen shot, the value is changed to 1990 and 1984 is what is returned - the default value.
Other references:
Upvotes: 0
Views: 756
Reputation: 8685
It sounds like the button click callback is being triggered before the manually-entered year has a chance to save to the DatePicker.
I believe the Month and Day fields "work" because when you fill in the value, they automatically refocus to the next field (which saves the value to the DatePicker). However, I was able to force the Day field to behave the way you describe by inputting a single-digit day, closing the keyboard, and then pressing "OK"—the day value was not saved.
Let's take a look at the source of DatePickerDialog's "OK" method:
public void onClick(DialogInterface dialog, int which) {
if (mCallBack != null) {
mDatePicker.clearFocus();
mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(),
mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
}
}
Since your buttons appear to be custom layout items (i.e. you're not using an actual DatePickerDialog), you should probably call clearFocus()
on your DatePicker before getting the date. Or, use a DatePickerDialog.
Upvotes: 4