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.
3 Answers
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