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>
Android TimePicker is a user interface control for selecting the time in either 24-hour format or AM/PM mode. It is used to ensure that users pick a valid time for the day in the application. The default TimePicker can be customized by using the SnapTimePicker in Android.
Android Time Picker allows you to select the time of day in either 24 hour or AM/PM mode. The time consists of hours, minutes and clock format. Android provides this functionality through TimePicker class.
TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity. this, new TimePickerDialog. OnTimeSetListener() { @Override public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) { } }, 0, 0, false); 9- Finally add the following code to be able to see TimePickerDialog appear in the screen.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With