I'm having some issues implementing a TimePicker in my application that allows the user to change the time of a database record prior to inserting it.
The problem is that when the AM/PM button is pressed, the onTimeChanged(View, int, int) method isn't invoked. Whenever I change either the hour or minute value of the TimePicker, onTimeChanged() is called, however.
Scenarios:
Am I wrong in thinking that the AM/PM button should be able to be clicked to update the time without having to also change a time value after the am/pm button?
I've put together a small test project to replicate this and here's the code:
Activity
public class TestActivity extends Activity implements OnTimeChangedListener {
private Calendar mCalendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timepicker);
mCalendar = Calendar.getInstance();
TimePicker tp = (TimePicker)findViewById(R.id.timepicker);
tp.setIs24HourView(false);
tp.setOnTimeChangedListener(this);
}
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
Log.d("TAG", "In onTimeChanged");
mCalendar.set(mCalendar.get(Calendar.YEAR),
mCalendar.get(Calendar.MONTH),
mCalendar.get(Calendar.DAY_OF_MONTH),
hourOfDay,
minute);
setCalendarTime();
}
private void setCalendarTime() {
Date date = mCalendar.getTime();
if (date != null) {
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy '@' h:mm a");
String dateTime = formatter.format(date);
Toast.makeText(this, dateTime, Toast.LENGTH_LONG).show();
}
}
}
timepicker.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<TimePicker android:id="@+id/timepicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"/>
</LinearLayout>
I have tested the code. Yes, you are right, TimePicker AM/PM button not invoking onTimeChanged method which it should.
Actually, Its a bug. It has been reported to google. You can find it here
http://code.google.com/p/android/issues/detail?id=18982
Please vote, comment & Star the bug report to raise its priority and to get it fixed by development team as soon as possible.