Skip to content

Instantly share code, notes, and snippets.

@john-lorrenz
Created February 17, 2020 01:36
Show Gist options
  • Save john-lorrenz/4165b921c3f4b4e898b0e3a43da32777 to your computer and use it in GitHub Desktop.
Save john-lorrenz/4165b921c3f4b4e898b0e3a43da32777 to your computer and use it in GitHub Desktop.
Date picker
import android.app.DatePickerDialog
import android.widget.EditText
import java.text.DecimalFormat
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
dateText.setOnClickListener {
setDatePickerAction(it as EditText)
openDatePicker()
}
private fun setDatePickerAction(){
// SET ACTION AFTER SELECTING THE DATE
mDateSetListener = DatePickerDialog.OnDateSetListener { datePicker, year, month, day ->
// FORMAT DAY and MONTH to always show two digits (add zero if single digit)
var mFormat = DecimalFormat("00");
var monthString = mFormat.format(month + 1)
var dayString = mFormat.format(day)
val dateInMilliseconds = convertDateToMilliseconds(year, month, day,12, 0, 0)
// PUT THE SELECTED DATE ON THE DATE PLACEHOLDER
dateText.setText("${year}-${monthString}-${dayString}")
// TODO: How to get current AGE??
// If todays month is greater than current month
// if todays day is greater than or equals to day
}
}
private fun openDatePicker() {
// INSTANTIATE CALENDAR
var cal = Calendar.getInstance()
cal.add(Calendar.YEAR, 0)
var year = cal.get(Calendar.YEAR)
var month = cal.get(Calendar.MONTH)
var day = cal.get(Calendar.DAY_OF_MONTH)
// INSTANTIATE DATE PICKER DIALOG
var dialog = DatePickerDialog(this, android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateSetListener, year, month, day)
// SET DATE PICKER DIALOG STYLE
dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
// SHOW THE DATE PICKER DIALOG
dialog.show()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment