androidkotlinmaterial-designmaterial-components-androidandroid-timepicker

How to pick time using material design?


how to pick a time using material:1.3.0-alpha01, I am looking time picker following screenshot, which I found in material io.

I tried date picker is working material time picker

Material Date picker

//Creating a MaterialDatePicker.Builder instance that gives us a single date selector.
val builder : MaterialDatePicker.Builder<*> = MaterialDatePicker.Builder.datePicker() // 1
//Building-up the MaterialDatePicker by calling a builder.build function
val picker : MaterialDatePicker<*> = builder.build()  // 2
//Display the Material Date Picker.
picker.show(requireActivity().supportFragmentManager, picker.toString())   // 3

Simple Time picker

  val cal = Calendar.getInstance()
            val timeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute ->
                cal.set(Calendar.HOUR_OF_DAY, hour)
                cal.set(Calendar.MINUTE, minute)
                //textView.text = SimpleDateFormat("HH:mm").format(cal.time)
            }
            val time = TimePickerDialog(context, timeSetListener, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true)
            time.setTitle("")
            time.show()

Solution

  • MATERIAL TIME PICKER To use MATERIAL TIME PICKER, add the following dependency to your project's build.gradle file:

       implementation 'com.google.android.material:material:1.3.0-alpha02'
    

    Kotlin time picker using material:1.3.0-alpha02

    val timePickerDialog = MaterialTimePicker.newInstance();
                timePickerDialog.show(requireActivity().supportFragmentManager, "fragment_tag");
                // Types of formats for the time pickeer
                timePickerDialog.setTimeFormat(TimeFormat.CLOCK_12H)//CLOCK_24H
                timePickerDialog.setListener { dialog: MaterialTimePicker ->
                    val newHour = dialog.hour
                    val newMinute = dialog.minute
    }