How to set TimePicker show with format 24h

I’ve created a TimePicker in layout and I want it to show time with format 24h.

Can you help me? Thanks.

  • window manager bad token exception
  • How do I get Android Studio to stop returning generated code in search results?
  • How to place an icon into the title of a custom dialog
  • How to start an Intent if context is not Activity Context but Application Context
  • TabHost w/ Activities vs. ActionBar w/ Fragments
  • Dismiss Ongoing Android Notification Via Action Button Without Opening App
  • Related posts:

    Running the new Intel emulator for Android
    Change status bar text color when primaryDark is white
    NEON intrinsic types work in C but throw invalid arguments error in C++
    Getting black ImageView using Picasso and Glide
    Android studio AssertionError: Don't invoke waitForSmartMode from inside read action in dum...
    android-library with specific Product Flavors dependency
  • Android Phonegap - How to Open Native Google Maps Application
  • Delay actions in android
  • HTML5 navigator.online does not work in WebView
  • Android ViewPager get the current View
  • Android Toast in iPhone?
  • How to remove the device's frame on Android Studio's emulators
  • 9 Solutions collect form web for “How to set TimePicker show with format 24h”

    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.

    Setting TimePicker by default to 24 hour mode is not always a good idea since many countrys have diffrent convention. For this case use DateFormat.is24HourFormat(getActivity()).

    Your final code migth look like picker.setIs24HourView(DateFormat.is24HourFormat(this));.

    Further details see:

    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 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);
    

    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); 
    

    By default, it displays time in the AM/PM format. If you want to change time in the 24 hour format, then you can use the setIs24HourView() method.
    See this link: http://www.androidaspect.com/2012/06/timepicker-view-tutorial.html

    You just need call and send true to setIs24HourView function.

    TimePicker tpHourMin = (TimePicker) findViewById(R.id.timePicker);
    tpHourMin.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/

    I need the same just for an application to set the duration spend for doing a task. I found that I just need to extend the TimePickerDialog class and pass True in the last constructor parameter boolean is24HourView.

    public class DurationPickerDialog extends TimePickerDialog {
    
    public DurationPickerDialog(Context context, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute){
        super(context, callBack, hourOfDay, minute, true);
    }
    

    Then you only need to create this as

    TimePickerDialog durationPicker =  new DurationPickerDialog(getActivity(), this, hour, minute);
    durationPicker.setTitle("Duration:");
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.