Afdal
Afdal

Reputation: 655

Date Picker Dialog not showing in Fragment

Date Picker Dialog not showing in Fragment

  1. I want to show date picker dialog if edittext is clicked
  2. show date from date picked to edittext
 class VerifikasiInfoPribadiFragment : BaseFragment() {
        override fun getLayoutResId(): Int = R.layout.fragment_verifikasi_info_pribadi

        override fun onActivityCreated(savedInstanceState: Bundle?) {
            super.onActivityCreated(savedInstanceState)
            edtTanggalLahir.setOnClickListener {
                showDatePicker()
            }
        }

        private fun showDatePicker() {
            val calendar = Calendar.getInstance()
            val year = calendar.get(Calendar.YEAR)
            val month = calendar.get(Calendar.MONTH)
            val dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)

            val datePicker = DatePickerDialog(
                requireActivity(),
                DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                    edtTempatLahir.setText("" + dayOfMonth + "/" + month + "/" + year)
                },
                year,
                month,
                dayOfMonth
            )
            datePicker.show()
        }
    }

i've try in Activity look good

Upvotes: 0

Views: 439

Answers (1)

aliraza12636
aliraza12636

Reputation: 473

Maybe it is happening because you called showDatePicker() in onActivityCreated() according to the life cycle, though the activity has created right now, the fragment might not be created yet so try calling the method inside onViewCreated() or onCreateView()

Upvotes: 1

Related Questions