Android: TimePicker setIs24HourView not working

I am trying to use TimePicker in a 24 hour format, and I am using setIs24HourView = true, but I still do not get the 24 hour format on TimePicker. Here is my code in onCreate of Activity.

timePicker = (TimePicker) findViewById(R.id.timePicker1); timePicker.setIs24HourView(true); 

I even tried timePicker.setCurrentHour (cal.HOUR_OF_DAY) after setIs24HourView, but I can not see the 24-hour format on TimePicker.

 <TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="110dp" /> 

Am I missing something here?

enter image description here

+6
source share
5 answers

ok, I see the problem now. It does not set currentHour correctly in the new 24-hour format, so you got 9:05 instead of 21:05. I guess it's not 9am where you are! This is a mistake, as mentioned in this question, it 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 that you tried cal.HOUR_OF_DAY, but did not provide the code that you used, but try this and see if it helps.

+9
source

I notice this problem in jelly bean You can set the display time in 24 hours

  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); 
+8
source

You can simply use the deviceโ€™s settings and thus allow this user to make this decision;

  String clockType = android.provider.Settings.System.getString(context.getContentResolver(), android.provider.Settings.System.TIME_12_24); 
0
source

setting timePicker.setCurrentHour(Calendar.get(Calendar.HOUR_OF_DAY)) solved the problem

0
source
 timePicker = (TimePicker) findViewById(R.id.timePicker1); timePicker.setIs24HourView(true); 

We need to add the following:

 Calendar calendar = Calendar.getInstance(); timePicker.setCurrentHour(calendar.get(calendar.HOUR_OF_DAY)); 
0
source

All Articles