23

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.

Pijusn
  • 11,025
  • 7
  • 57
  • 76

3 Answers3

44

No, you can't set the 24 hours mode in the XML, you have to use

MyTimePicker.setIs24HourView(boolean);
Beryllium
  • 12,808
  • 10
  • 56
  • 86
Guillaume
  • 8,741
  • 11
  • 49
  • 62
15

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"/>
frogatto
  • 28,539
  • 11
  • 83
  • 129
FINARX
  • 248
  • 3
  • 5
1

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
reza.cse08
  • 5,938
  • 48
  • 39