Reputation: 11
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:
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
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