Search code examples
androidkotlinandroid-datepicker

How to show only day and month in date picker dialog in Kotlin(Android)?


I have a datepicker dialog. I only want to show day and month. Year picker dialog must hidden. I already tries other answers like this . Nothing works for me. It should also support for Kitkat to Nougat devices. My datepicker code follows.

     fun setDatePickerDialog() {
        mDobDialog = DatePickerDialog(this@SignUpActivity, R.style.VDDatePickerDialogTheme, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
            val newDate = Calendar.getInstance()
            newDate.set(year, monthOfYear, dayOfMonth)
            //dob_textview.setText("($dayOfMonth)  ($monthOfYear)  ($year)")
            val dateFormat = SimpleDateFormat(VDAppConstants.DOB_DISPLAY_FORMAT)
            dob_textview?.setText(dateFormat.format(newDate.time))

        }, mNewCalendar.get(Calendar.YEAR), mNewCalendar.get(Calendar.MONTH), mNewCalendar.get(Calendar.DAY_OF_MONTH))

        mNewCalendar.set(1978,
                mNewCalendar.get(Calendar.MONTH),
                mNewCalendar.get(Calendar.DAY_OF_MONTH))
        mDobDialog?.datePicker?.maxDate = mNewCalendar.timeInMillis
    }

The following code only works on Kitkat devices but not working on Nougat Devices.

 val mDobDialog = DatePickerDialog(this@MainActivity, android.R.style.Theme_Holo_Dialog, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
        // Display Selected date in textbox
        //date.setText("" + dayOfMonth + " " + monthOfYear + ", " + year)
    }, year, month, day)
    mDobDialog.show()


    // Hide Year Selector in Date Picker
    mDobDialog.findViewById(Resources.getSystem().getIdentifier("year", "id", "android")).visibility = View.GONE

Solution

  • I tested with different code and this code works fine. If you use datepicker theme as Theme_Holo_Dialog then it working fine. Working code as per below.

    Note: It's not working if you set theme Theme_Material_Dialog

    package com.wave18.datepickedialogdemo
    
    import android.annotation.SuppressLint
    import android.app.DatePickerDialog
    import android.content.res.Resources
    import android.os.Bundle
    import android.support.v7.app.AppCompatActivity
    import android.view.View
    import kotlinx.android.synthetic.main.activity_main.*
    import java.util.*
    
    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            // Date Picker Dialog
            val dialog = datePickerDialog()
    
            // Button for Showing Date Picker Dialog
            button_show_date_picker.setOnClickListener {
    
                // Show Date Picker
                dialog.show()
    
                // Hide Year Selector
                val year = dialog.findViewById<View>(Resources.getSystem().getIdentifier("android:id/year", null, null))
                if (year != null) {
                    year.visibility = View.GONE
                }
    
    
            }
    
    
        }
    
        // Function for Showing Date Picker
        @SuppressLint("SetTextI18n")
        fun datePickerDialog(): DatePickerDialog {
            val c = Calendar.getInstance()
            val year = c.get(Calendar.YEAR)
            val month = c.get(Calendar.MONTH)
            val day = c.get(Calendar.DAY_OF_MONTH)
    
            // Date Picker Dialog
            val datePickerDialog = DatePickerDialog(this@MainActivity, android.R.style.Theme_Holo_Dialog, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
                // Display Selected date in textbox
                date.text = "$dayOfMonth $monthOfYear, $year"
            }, year, month, day)
            // Show Date Picker
    
            return datePickerDialog
    
    
        }
    
    }
    

    Date Picker Dialog without year selector