Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Android TimePicker In XML

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.

like image 625
Pijusn Avatar asked Jul 22 '11 13:07

Pijusn


People also ask

What is use TimePicker in Android?

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.

What are the two formats of TimePicker How do you set them?

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.

Which method is used to set 24 hrs in TimePicker dialog box?

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.


3 Answers

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

MyTimePicker.setIs24HourView(boolean);
like image 108
Guillaume Avatar answered Oct 13 '22 00:10

Guillaume


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"/>
like image 32
FINARX Avatar answered Oct 13 '22 01:10

FINARX


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
like image 1
reza.cse08 Avatar answered Oct 13 '22 02:10

reza.cse08