I\'ve created a TimePicker in layout and I want it to show time with format 24h.
Can you help me? Thanks.
TimePicker
is a view for selecting the time of day, in either 24 hour or AM/PM mode.
You can use setIs24HourView(true) method with TimePicker.
xml
<TimePicker
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/timePicker" />
java
TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
As some people have already mentioned just set the parameter to true or false.
timePicker.setIs24HourView(true);
You can find a complete example with source code to download from here http://www.ahotbrew.com/android-timepicker-example/
You just have to retrieve the TimePicker instance from the view after inflating it, then you can modify the widget.
Ids are arbitrary:
View v=getActivity().getLayoutInflater().inflate(R.layout.dialog_time, null);
TimePicker timePicker=(TimePicker)v.findViewById(R.id.dialog_time_timePicker);
timePicker.setIs24HourView(true);
If you want the TimePicker to be correctly initialized with the current time in 24h format use the following:
import java.util.Calendar;
timePicker.setIs24HourView(true);
timePicker.setCurrentHour(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
Otherwise, due to Android bug, the picker will start with an incorrect hour (2 instead of 14 etc).
You just need call and send true to setIs24HourView function.
TimePicker tpHourMin = (TimePicker) findViewById(R.id.timePicker);
tpHourMin.setIs24HourView(true);