Search code examples
androidkotlinandroid-fragmentsmaterial-designtimepicker

Material Time Picker - supportFragmentManager not detected


I am trying to use the material date and time picker and have managed to refer and put together the code below to display date and time picker on button click. Have also used the latest alpha version of Material design 1.3.0-alpha03. But while supportFragmentManager is working for the MaterialDatePicker - it is being marked as Unresolved for MaterialTimePicker. Cannot seem to understand why this is happening... can somebody please explain and guide for the solution to this issue? My code is below

        editEventEndDate.setOnClickListener {
        // Create the date picker builder and set the title
        val builder = MaterialDatePicker.Builder.datePicker()
            .also {
                title = "Pick Date"
            }
        // create the date picker
        val materialDatePicker = builder.build()
        // set listener when date is selected
        materialDatePicker.addOnPositiveButtonClickListener {
            // Create calendar object and set the date to be that returned from selection
            val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
            calendar.time = Date(it)
            val endDate = Calendar.DATE
            val endYear = Calendar.YEAR
            val endDay = Calendar.DAY_OF_WEEK
            val endMonth = Calendar.MONTH
            val endResult =  "$endDay, $endDate $endMonth $endYear"
            editEventStartDate.text = endResult
        }
        materialDatePicker.show(supportFragmentManager, "MyTAG")
    }

    //Add time on click
    editEventStartTime.setOnClickListener {
        val materialTimePicker = MaterialTimePicker.Builder()
            .setTimeFormat(clockFormat)
            .setHour(hour)
            .setMinute(minute)
            .also {
                title = "Pick Time"
            }
            .build()
        materialTimePicker.show(**supportFragmentManager()**, "fragment_tag")
        materialTimePicker.addOnPositiveButtonClickListener {
            val newHour = materialTimePicker.hour
            val newMinute = materialTimePicker.minute
            [email protected](newHour, newMinute)
        }
    }

Solution

  • supportFragmentManager() is not a function and the error is correct . use supportFragmentManager