Phy
Phy

Reputation: 11

Android circular Time Picker does not accept 12 o'clock

I have a strange problem with the Android L time picker. In 24 hours format it is not possible to set the hours to 12. Instead 00 is set:

  1. Circular time picker dialog opens
  2. I tap on the 12 to set the hour
  3. Instad of "12" hours "00" hours is displayed at the top of the dialog
  4. I choose the minutes without problems, e.g. 15
  5. I click Done and instad of 12:15 my text view displays 00:15

I tried the Google example implementation, with no difference. 12-hours mode does work without problems.

App info:

Has someone an idea what could be the cause of that problem?

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    // Create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), this, hour, minute,
            DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
}
}

Upvotes: 1

Views: 705

Answers (1)

Niaj Mahmud
Niaj Mahmud

Reputation: 469

Try this. just include only four condition

 private fun showTimePicker2() {
        picker2 = MaterialTimePicker.Builder()
            .setTimeFormat(TimeFormat.CLOCK_12H)
            .setHour(12)
            .setMinute(0)
            .setTitleText("Select Alarm Time")
            .build()

        picker2.show(supportFragmentManager, "2222")
        picker2.addOnPositiveButtonClickListener {
            if (picker2.hour == 12 && picker2.minute > 0) {
                textTime2_id.text =
                    String.format("%02d", picker2.hour) + " : " + String.format(
                        "%02d",
                        picker2.minute
                    ) + "PM"
            } else if (picker2.hour ==0) {
                textTime2_id.text =
                    String.format("%02d", picker2.hour + 12) + " : " + String.format(
                        "%02d",
                        picker2.minute
                    ) + "AM"
            }else if (picker2.hour > 12) {
                textTime2_id.text =
                    String.format("%02d", picker2.hour - 12) + " : " + String.format(
                        "%02d",
                        picker2.minute
                    ) + "PM"
            } else {
                textTime2_id.text = String.format("%02d", picker2.hour) + " : " + String.format(
                    "%02d",
                    picker2.minute
                ) + "AM"
            }
            calender2 = Calendar.getInstance()
            calender2.set(Calendar.HOUR_OF_DAY, picker2.hour)
            calender2.set(Calendar.MINUTE, picker2.minute)
            calender2.set(Calendar.SECOND, 0)
            calender2.set(Calendar.MILLISECOND, 0)

            Log.d("this", "showTimePicker2" + calender2.set(Calendar.DAY_OF_WEEK, 1).toString())

        }
    }

Upvotes: 0

Related Questions