Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TimePicker AM/PM button not invoking onTimeChanged

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:

  • User just clicks the AM/PM button: AM/PM is NOT updated
  • User clicks the Hours/Minutes: Time is updated
  • User clicks the AM/PM button then changes the hours/minutes: Time and AM/PM is updated

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>
like image 259
hooked82 Avatar asked Jan 28 '12 17:01

hooked82


People also ask

How to customize time picker in Android?

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.

How to select time 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.

How to open TimePicker on edittext click in Android?

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.


1 Answers

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.

like image 184
Vivek Avatar answered Sep 22 '22 16:09

Vivek