Reputation: 2826
I'm working through the Big Nerd Ranch Android programming book. I'm working on the CriminalIntent project and am trying to setup a DatePicker and TimePicker to set the date and time of the "crime." My DatePicker dialog works fine, when the date is changed, everything updates properly, no issues there.
The problem comes in the TimePicker. For some reason, the onTimeChangedListener doesn't seem to be firing, and thus, the time doesn't update properly. I've copied in my code for setting the listener to the TimePicker. I have a log statement that never fires when I change the time, which is why I think this is where the issue is.
TimePicker timePicker = (TimePicker) v.findViewById(R.id.dialog_time_picker);
timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(minute);
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// Get the date
date = new GregorianCalendar(year, month, day, hourOfDay, minute).getTime();
Log.d("TimePickerFragment", "hour: " + hourOfDay + "; minute: " + minute);
// Save this date
getArguments().putSerializable(EXTRA_TIME, date);
}
});
And here is the code where I set the hour and minute:
date = (Date) getArguments().getSerializable(EXTRA_TIME);
// Create a calendar to get hour, minute, and AM/PM info
cal = Calendar.getInstance();
cal.setTime(date);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int amPm = cal.get(Calendar.AM_PM); // 0 = AM 1 = PM
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);
If you need any other code let me know
Upvotes: 2
Views: 2706
Reputation: 2814
Prior to Lollipop(5.0), TimePicker view would fire an onTimeChanged event each time when the time changed, allowing an app to dynamically react to the time currently specified by the picker.
In Lollipop, the Timepicker came with
android:timePickerMode="clock"
In Clock mode, the onTimeChanged events is not getting fired, when the time is changed.
So the Optimal way is better go with android:timePickerMode="spinner"
<TimePicker
android:id="@+id/timepick_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:timePickerMode="spinner"/>
Upvotes: 5
Reputation: 18112
Use TimePickerDialog
and OnTimeSetListener
;put your code inside listener's method onTimeSet
. This will be called when user choose a time.
Calendar mcurrentDate = Calendar.getInstance();
int mHour = mcurrentDate.get(Calendar.HOUR_OF_DAY);
int mMinute = mcurrentDate.get(Calendar.MINUTE);
TimePickerDialog mTimePicker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
date = new GregorianCalendar(year, month, day, hourOfDay, minute).getTime();
Log.d("TimePickerFragment", "hour: " + hourOfDay + "; minute: " + minute);
getArguments().putSerializable(EXTRA_TIME, date);
}
}, mHour, mMinute, true);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
Upvotes: 0