Android: TimePicker setIs24HourView not working
Solution 1
ok, I see what the problem is now. It's not correctly setting the currentHour to the new 24 hour format, which is why you got 9:05 instead of 21:05. I'm guessing it isn't 9am where you are! It is a bug, as mentioned in this question seems the only work around, for now, is to do something like:
timePicker = (TimePicker) findViewById(R.id.timePicker1);
timePicker.setIs24HourView(true);
timePicker.setCurrentHour(Calendar.get(Calendar.HOUR_OF_DAY));
I see you tried cal.HOUR_OF_DAY, but didn't provide the code you actually used, but try this and see if it helps.
Solution 2
I notice this problem in jelly bean You can set the time to show in 24 hour view
TimePicker picker = (TimePicker) findViewById(R.id.timePicker1);
picker.setIs24HourView(true);
Calendar calendar = Calendar.getInstance();
int h = calendar.get(Calendar.HOUR_OF_DAY);
int m = calendar.get(Calendar.MINUTE);
picker.setCurrentHour(h);
picker.setCurrentMinute(m);
Sammys
Updated on June 07, 2022Comments
-
Sammys almost 2 years
I am trying to use the TimePicker in the 24 hour format and I am using setIs24HourView= true, but I am still not getting 24 hour format on the TimePicker. Here is my code in the onCreate of the Activity.
timePicker = (TimePicker) findViewById(R.id.timePicker1); timePicker.setIs24HourView(true);
I even tried timePicker.setCurrentHour(cal.HOUR_OF_DAY) after setIs24HourView, but I am not able to see the 24 hour format on the TimePicker.
<TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="110dp" />
Am I missing anything here?
-
Sammys about 11 yearsthank you! setting timePicker.setCurrentHour(Calendar.get(Calendar.HOUR_OF_DAY)) worked!
-
Sammys about 11 yearsSorry, I am new to this. Accepted your answer now.
-
Marcos over 10 yearsBut
get
is not a static method ... how this could work? You must instantiate Calendar withgetInstance
and then callget
. -
Gaurav Pansheriya over 10 yearshere this code set HOUR_OF_DAY is o to 11 and leave screen and come back than hour is automatically change check it.. you set 0 than display 12 after enter screen again..
-
Gaurav Pansheriya over 10 yearsThat code is not Working than HOUR_OF_DAY is 0 because that Time Picker is display automatically 12..
-
Bob Ueland over 8 years@enrmarc is right, you must do it like this: Calendar calendar = Calendar.getInstance(); timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));