user205650
user205650

Reputation: 1

DatePicker in Kotlin showing wrong month

When implementing Datepicker in kotlin instead of July its showing M07 in all devices.

enter image description here

Here is code for Date Picker:

val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog(activity, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth -> //differnce(dayOfMonth, monthOfYear, year, false) }, year, month, day) datePickerDialog.show()

Upvotes: 0

Views: 2361

Answers (3)

MeesamGardezi
MeesamGardezi

Reputation: 11

datebtn.setOnClickListener {
            val c= Calendar.getInstance()
            val year= c.get(Calendar.YEAR)
            val month = c.get(Calendar.MONTH)
            val day = c.get(Calendar.DAY_OF_MONTH)
            var dpd = DatePickerDialog(this,DatePickerDialog.OnDateSetListener { view, mYear,mMonth , mDay ->
                val mmMonth = mMonth+1
                val date = "$mDay/$mmMonth/$mYear"
                datetv.setText(date)
            },year,month,day)
            dpd.show()
        }

Upvotes: 1

David
David

Reputation: 167

Try replacing

{ view, year, monthOfYear, dayOfMonth -> //differnce(dayOfMonth, monthOfYear, year, false) }

With

{ view, year, monthOfYear, dayOfMonth -> this.year = year this.month = monthOfYear this.dayOfMonth = dayOfMonth}

Upvotes: 0

Android Geek
Android Geek

Reputation: 9225

try this:

class MainActivity : AppCompatActivity() {

var button_date: Button? = null
var textview_date: TextView? = null
var cal = Calendar.getInstance()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // get the references from layout file
    textview_date = this.text_view_date_1
    button_date = this.button_date_1

    textview_date!!.text = "--/--/----"

    // create an OnDateSetListener
    val dateSetListener = object : DatePickerDialog.OnDateSetListener {
        override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
                               dayOfMonth: Int) {
            cal.set(Calendar.YEAR, year)
            cal.set(Calendar.MONTH, monthOfYear)
            cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)
            updateDateInView()
        }
    }

    // when you click on the button, show DatePickerDialog that is set with OnDateSetListener
    button_date!!.setOnClickListener(object : View.OnClickListener {
        override fun onClick(view: View) {
            DatePickerDialog(this@MainActivity,
                    dateSetListener,
                    // set DatePickerDialog to point to today's date when it loads up
                    cal.get(Calendar.YEAR),
                    cal.get(Calendar.MONTH),
                    cal.get(Calendar.DAY_OF_MONTH)).show()
        }

    })
}

private fun updateDateInView() {
    val myFormat = "MM/dd/yyyy" // mention the format you need
    val sdf = SimpleDateFormat(myFormat, Locale.US)
    textview_date!!.text = sdf.format(cal.getTime())
}

enter image description here

show calendar like this please try

Upvotes: 0

Related Questions