2

Is there any way to remove the AM/PM in a Time Picker Widget?

enter image description here

I have this function in my application but its purpose is to select only Hour and Minutes not including AM/PM, I tried to setIs24HourView(true) but it makes the time 24hours, I only want 12 hours.

Polar
  • 3,327
  • 4
  • 42
  • 77
  • Yeah, it looks like the only way `TimePicker` will hide that on its own is if it's in 24-hour mode. See if [this code](http://pastebin.com/LJSN75L7) works for ya, before I post it. I can't test it, at the moment. – Mike M. Aug 22 '16 at 04:21
  • 1
    @MikeM. -> and it's works! Thank you! Please make it Answer instead of comment, Also this can help the others so if you put the code in Answer instead of link that will be very appreciated. Thank you. – Polar Aug 22 '16 at 05:25

1 Answers1

5

It seems there is no public method in TimePicker to directly hide or show the AM/PM chooser. However, a look at the source code will give us the name of the resource ID for that View, which we can get with the system Resources. Then it's simply a matter of finding the View, and setting its visibility to GONE.

private void hideAmPmLayout(TimePicker picker) {
    final int id = Resources.getSystem().getIdentifier("ampm_layout", "id", "android");
    final View amPmLayout = picker.findViewById(id);
    if(amPmLayout != null) {
        amPmLayout.setVisibility(View.GONE);
    }
}
Mike M.
  • 38,532
  • 8
  • 99
  • 95