Search code examples
androidtimepicker

TimePicker Clock mode below Lollipop


I am trying to make my TimePicker appear as a clock. I use timepickermode="clock" but I get a lint warning that says that it's compatible with api level 21 and above.

I've seen this feature ran on my kitkat in Todoist app. How else do I implement this?


Solution

  • You can't use the TimePicker with clock look in version prior Lollipop as stated by the documentation:

    timePickerMode
    Defines the look of the widget. Prior to the L release, the only choice was spinner. As of L, with the Material theme selected, the default layout is clock, but this attribute can be used to force spinner to be used instead.

    There is an alternative offered by Google itself. It is an external datepicker project that is compatible with API level 14 and higher. You can find the project here:

    https://android.googlesource.com/platform/frameworks/opt/datetimepicker/

    Once you clone the repo, add it to your existing project, then you can use the TimePicker like this:

    TimePickerDialog timePickerDialog = new TimePickerDialog();
    timePickerDialog.setOnTimeSetListener(new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
    
        }
    });
    timePickerDialog.show(getFragmentManager(), fragmentTag);
    

    Be careful to import the com.android.datetimepicker.time.TimePickerDialog not the standard android.app.TimePickerDialog.