20

I was testing my app on an Android L emulator, and I noticed that the TimePickerDialog has significantly changed to this:

Android L TimePickerDialog

This doesn't fit with the theme of my app, and I wanted to know if it is possible to get the old TimePickerDialog style when running on Android L.

JDJ
  • 4,298
  • 3
  • 25
  • 44
  • 2
    have you tried using the constructor with theme parameter? http://developer.android.com/reference/android/app/TimePickerDialog.html#TimePickerDialog(android.content.Context, int, android.app.TimePickerDialog.OnTimeSetListener, int, int, boolean) – LordRaydenMK Jun 27 '14 at 10:38
  • Wow I can't believe I missed that! – JDJ Jun 27 '14 at 10:41
  • 2
    Now I can use a TimePicker that doesn't look like it was designed by a child clown. – JDJ Jun 27 '14 at 10:42
  • I'll add an answer so you can mark the question as answered – LordRaydenMK Jun 27 '14 at 10:56

5 Answers5

43

Add android:timePickerMode="spinner" to the XML

    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:timePickerMode="spinner">
    </TimePicker>

https://developer.android.com/reference/android/R.attr.html#timePickerMode

You can choose between spinner or clock modes. This attribute is only available from API level 21, before that the spinner mode was the only option available.

yehyatt
  • 2,295
  • 3
  • 28
  • 31
  • There is no timePickerMode as far as I am aware. You can only do this for a datepicker (where it is called 'android:datePickerMode'). – Lieuwe Jul 05 '15 at 08:54
  • There is actually a timePicker , [http://developer.android.com/reference/android/widget/TimePicker.html](http://developer.android.com/reference/android/widget/TimePicker.html) , The code about is from a working example . – yehyatt Jul 06 '15 at 08:34
  • 1
    I didn't say there is no TimePicker. I said that I believe there is no android:timePickerMode (and the link you provide indeed makes no mention of this) – Lieuwe Jul 06 '15 at 09:22
  • 2
    Sorry for the previous link , but am pretty sure there is a timePickerMode, its available from API Level 21 and here is the correct link https://developer.android.com/reference/android/R.attr.html#timePickerMode – yehyatt Jul 06 '15 at 10:55
  • Nice one - that feature is well hidden. Note that using the style you also have the classic option (with the buttons) available and not just clock or spinner. – Lieuwe Jul 07 '15 at 09:53
  • @Lieuwe Pretty sure there is a timePickerMode. Please verify before you make your statement as this answer is actual the one that works. – Kyle Huang Jan 17 '17 at 02:37
16

You can use the TimePickerDialog constructor with theme parameter to specify the theme.

TimePickerDialog(Context context, 
                 int theme, 
                 TimePickerDialog.OnTimeSetListener callBack, 
                 int hourOfDay, 
                 int minute, 
                 boolean is24HourView)
JDJ
  • 4,298
  • 3
  • 25
  • 44
LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56
16

If you want the AM / PM to be a picker instead of a button then I recommend you use the single parameter constructor with a ContextThemeWrapper:

new TimePicker(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light_Dialog_NoActionBar));

AM-PM picker

If you pass the theme into the multiple parameter constructor then you'll end up with a rather ugly AM / PM button, and the colon will be missing between the hour and minute columns.

new TimePicker(getActivity(), null, TimePickerDialog.THEME_HOLO_LIGHT);

AM-PM button

Dan J
  • 25,433
  • 17
  • 100
  • 173
10

As LordRaydenMK said, use the TimePickerDialog constructor with theme parameter and provide this theme: android.R.style.CustomDatePickerDialog

And in your Styles.xml define this:

  <style name="CustomDatePickerDialog"
         parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:dialogTheme">@style/MyDialogTheme</item>
    <item name="android:timePickerStyle">@style/MyDatePicker</item>
  </style>

  <style name="MyDialogTheme"
         parent="Theme.AppCompat.Light.Dialog">
    <item name="android:timePickerStyle">@style/MyDatePicker</item>
  </style>

  <style name="MyDatePicker"
         parent="@android:style/Widget.Material.Light.TimePicker">
    <item name="android:timePickerMode">spinner</item>
  </style>
David Riha
  • 1,368
  • 14
  • 29
-3

You can use android:timePickerMode attribute in the XML.

For more info: Read Here

Prateek Sharma
  • 312
  • 3
  • 5