Agung
Agung

Reputation: 13803

how to set date picker in fragment using Kotlin?

I want to show date picker dialog using this library https://github.com/wdullaer/MaterialDateTimePicker

implementation "com.wdullaer:materialdatetimepicker:3.6.4"

here is my code in my fragment

import com.wdullaer.materialdatetimepicker.date.DatePickerDialog

        val now = Calendar.getInstance()
        val currentYear: Int = now.get(Calendar.YEAR)
        val currentMonth: Int = now.get(Calendar.MONTH)
        val currentDay: Int = now.get(Calendar.DAY_OF_MONTH)

        val datePickerDialog = DatePickerDialog.newInstance(DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->

            // do something here


        }, currentYear, currentMonth, currentDay)

        datePickerDialog.setTitle("INI JUDUL")
        datePickerDialog.setAccentColor(resources.getColor(R.color.colorPrimary))
        datePickerDialog.setOkText("SIP")
        datePickerDialog.setCancelText("GA JADI")
        datePickerDialog.show(fragmentManager,"")

but I have an error when I want to show that date picker dialog in this line datePickerDialog.show(fragmentManager,""), like this

enter image description here

I believe I have supplied the correct argument, but still....it gives an error

here is the fragment manager type: enter image description here

but if I force to unwrap like this, it still gives error enter image description here

Upvotes: 0

Views: 5270

Answers (3)

Cuong Nguyen
Cuong Nguyen

Reputation: 1018

With androidX, You should use newest version:

implementation 'com.wdullaer:materialdatetimepicker:4.2.3'

And in fragment:

fragmentManager?.let { manager ->
            datePickerDialog.show(manager, "DatePickerDialog")
        }

Upvotes: 1

Anton Sarmatin
Anton Sarmatin

Reputation: 525

Just tried this one and it works perfectly:

    val now = Calendar.getInstance()
    val currentYear: Int = now.get(Calendar.YEAR)
    val currentMonth: Int = now.get(Calendar.MONTH)
    val currentDay: Int = now.get(Calendar.DAY_OF_MONTH)

    val datePickerDialog = DatePickerDialog.newInstance(DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->

        // do something here


    }, currentYear, currentMonth, currentDay)

    datePickerDialog.setTitle("INI JUDUL")
    datePickerDialog.setAccentColor(resources.getColor(R.color.colorPrimary))
    datePickerDialog.setOkText("SIP")
    datePickerDialog.setCancelText("GA JADI")
    datePickerDialog.show(parentFragmentManager,"")

Try to clean project and invalidate cache. It might help.

Upvotes: 0

Jahanvi Kariya
Jahanvi Kariya

Reputation: 397

I don't know it's perfect way or not I have tried this code and it's working but method is deprecated. Let me know if you find another way.

val now = Calendar.getInstance()
        val currentYear: Int = now.get(Calendar.YEAR)
        val currentMonth: Int = now.get(Calendar.MONTH)
        val currentDay: Int = now.get(Calendar.DAY_OF_MONTH)

        val datePickerDialog = DatePickerDialog.newInstance(DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->

            // do something here


        }, currentYear, currentMonth, currentDay)

        datePickerDialog.setTitle("INI JUDUL")
        datePickerDialog.setAccentColor(resources.getColor(R.color.colorPrimary))
        datePickerDialog.setOkText("SIP")
        datePickerDialog.setCancelText("GA JADI")
        datePickerDialog.show(activity!!.fragmentManager, "Datepickerdialog"); 

Hope it will work!

Upvotes: 0

Related Questions