Is it possible to set TimePicker hours mode to 24-hours-mode in XML file? Or is it posible in Java only? I want to make a layout that has 24-hours picker but I can't find such attribute.
Android App Development for Beginners Android Time Picker allows you to select the time of day in either 24 hour or AM/PM mode. The time consists of hours, minutes and clock format. Android provides this functionality through TimePicker class.
Android TimePicker is a user interface control for selecting the time in either 24-hour format or AM/PM mode. It is used to ensure that users pick the valid time for the day in our application. In android, TimePicker is available in two modes first one is clock mode and another one is spinner mode.
setIs24HourView(Boolean is24HourView): This method is used to set the mode of the Time picker either 24 hour mode or AM/PM mode. In this method we set a Boolean value either true or false.
No, you can't set the 24 hours mode in the XML, you have to use
MyTimePicker.setIs24HourView(boolean);
It is possible by subclassing TimePicker
to get a 24hour version, and using that sub class in the XML file with the appropriate package name:
public class TimePicker24Hours extends TimePicker {
public TimePicker24Hours(Context context) {
super(context);
init();
}
public TimePicker24Hours(Context context,
AttributeSet attrs) {
super(context, attrs);
init();
}
public TimePicker24Hours(Context context,
AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setIs24HourView(true);
}
}
in the layout you may add
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:dt_codecomponents="http://schemas.android.com/apk/res/my.app.package"
...
<my.app.package.style.TimePicker24Hours
android:id="@+id/timePicker1"
android:layout_width="wrap_content"/>
You can set it from java file.
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); // to set 24 hours mode
timePicker.setIs24HourView(false); // to set 12 hours mode
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With