'TimePickerDialog in spinner mode

In my project, I use TimepickerDialog to show the time in a TextView. My Timepickerdialog is shown as a clock, but I want it shown as a spinner. How can I do this?



Solution 1:[1]

use this code show time picker as spinner

    public void showCustomTimePicker()
    {

          final Calendar myCalender = Calendar.getInstance();
          int hour = myCalender.get(Calendar.HOUR_OF_DAY);
          int minute = myCalender.get(Calendar.MINUTE);


            TimePickerDialog.OnTimeSetListener myTimeListener = new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    if (view.isShown()) {
                        myCalender.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        myCalender.set(Calendar.MINUTE, minute);

                    }
                }
            };
            TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_NoActionBar, myTimeListener, hour, minute, true);
            timePickerDialog.setTitle("Choose hour:");
            timePickerDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            timePickerDialog.show();
     }

Solution 2:[2]

Use this code

  TimePickerDialog picker = new TimePickerDialog(MainActivity.this,android.R.style.Theme_Holo_Light_Dialog_NoActionBar,
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker tp, int sHour, int sMinute) {
                    //eText.setText(sHour + ":" + sMinute);
                }
            }, 2, 3, false);
    picker.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
           picker .show();

Solution 3:[3]

Kotlin:

I used the below code to get the Timepicker in spinner mode.

private fun showTimePicker() {
        // Use the current time as the default values for the picker
        val c = Calendar.getInstance()
        val hour = c.get(Calendar.HOUR_OF_DAY)
        val minute = c.get(Calendar.MINUTE)
        // date picker dialog
        val timeSetListener = TimePickerDialog.OnTimeSetListener { timePicker, hour, minute ->
            c.set(Calendar.HOUR_OF_DAY, hour)
            c.set(Calendar.MINUTE, minute)
            mSelectedTime = SimpleDateFormat("HH:mm").format(c.time)
            root.tvTime.text = mSelectedTime
        }
        val timePickerDialog = TimePickerDialog(requireContext(),AlertDialog.THEME_HOLO_LIGHT, timeSetListener, hour, minute,
            DateFormat.is24HourFormat(requireContext()))
        timePickerDialog.show()
    }

Solution 4:[4]

All you need is to specify a theme and add the next style property

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="TimePicker" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:timePickerMode">spinner</item>
    </style>
</resources>

here are some related info

Solution 5:[5]

Add android:timePickerMode = "1" for spinner mode

and

android:timePickerMode="2" for clock mode.

Edit

Use a theme in constructor of TimePickerDialog like this

TimePickerDialog timePickerDialog = new TimePickerDialog(DateTimePickerDialogActivity.this, android.R.style.Theme_Holo_Light_Dialog, onTimeSetListener, hour, minute, is24Hour);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Shahzain ali
Solution 3 Yogesh Nikam Patil
Solution 4
Solution 5