I'm trying to set a datepicker where the user will choose a date and if it's under 18 years old and if not it will show a message.
Until now I have that the date picker can't choose future dates, but I don't know how to make the condition to ckeck if the user's date has under 18 years. Any help would be appreciated.
fun setupBirthdateDatePicker() {
val c: Calendar = Calendar.getInstance()
val year: Int = c.get(Calendar.YEAR)
val month: Int = c.get(Calendar.MONTH)
val day: Int = c.get(Calendar.DAY_OF_MONTH)
setClickListener {
val dialog = if (textInput.text.isNullOrEmpty()) {
DatePickerDialog(context,
{ view, year, month, dayOfMonth ->
textInput.setText(Utils.getDateFromDatePicker(year, month, dayOfMonth))
}, year, month, day)
} else {
DatePickerDialog(context,
{ view, year, month, dayOfMonth ->
textInput.setText(Utils.getDateFromDatePicker(year, month, dayOfMonth))
},
Utils.getYearFromDate(textInput.text.toString()),
Utils.getMonthFromDate(textInput.text.toString()),
Utils.getDayFromDate(textInput.text.toString()))
}
dialog.datePicker.maxDate = c.timeInMillis
dialog.show()
}
}
If you want 18 years ago in epoch milliseconds to set as the max/starting date of your picker, you can use:
(ZonedDateTime.now() - Period.ofYears(18)).toInstant().toEpochMilli()
Or if you want to validate, you can compare like this:
val eighteenYearsAgo = LocalDate.now() - Period.ofYears(18)
val listener = DatePickerDialog.OnDateSetListener { _, year, month, day ->
// +1 month because DatePicker's month is unfortunately zero-based
val pickedDate = LocalDate.of(year, month + 1, day)
if (pickedDate < eighteenYearsAgo) {
// Picked a date less than 18 years ago
}
}
Related
I am learning Kotlin on my own. I am trying to calculate the age of a user after the have input their date of Birth and display it in another activity.
I tried a bunch of different stuff and none worked. I'm sure I maybe overlooking something simple.
my code:
class MainActivity : AppCompatActivity() {
var date1: EditText? = null
var datePickerDialog: DatePickerDialog? = null
lateinit var submitButton: Button
lateinit var userInput: EditText
lateinit var dob: EditText
#SuppressLint("SetTextI18n", "MissingInflatedId", "CutPasteId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(layout.activity_main)
date1 = findViewById<EditText>(id.date) as EditTe
date1!!.setOnClickListener{ // calender class's instance and get current date , month and year from calender
val c = Calendar.getInstance()
val mYear = c[Calendar.YEAR] // current year
val mMonth = c[Calendar.MONTH] // current month
val mDay = c[Calendar.DAY_OF_MONTH] // current day
datePickerDialog = DatePickerDialog(
this#MainActivity,
{ view, year, monthOfYear, dayOfMonth -> // set day of month , month and year value in the edit text
date1!!.setText(
dayOfMonth.toString() + "/"
+ (monthOfYear + 1) + "/" + year
)
}, mYear, mMonth, mDay
)
datePickerDialog!!.show()
}
submitButton = findViewById(id.sub_btn)
userInput = findViewById(id.username1)
dob = findViewById(id.date)
submitButton.setOnClickListener {
val age= dob.text.toString()
val name= userInput.text.toString()
//val str = userInput.text.toString()
intent = Intent(this, CardReturn::class.java)
intent.putExtra("message_key","Name:$name")
intent.putExtra("message_key1","DOB:$age")
startActivity(intent)
}
}}
If you're able to utilize java.time or at least ThreeTen Android Backport, it should be easy and will save you from a lot of work if your'e using java.util.Calendar.
Here's a small working piece of DatePickerDialog that you can copy and paste easily, this is how I calculate year difference between two LocalDate instance
val now = LocalDateTime.now()
val initYear = now.year
val initMonth = now.monthValue - 1 // offset it -1 because January starts at 0 index
val initDay = now.dayOfMonth
val datePickerDialog = DatePickerDialog(
this#ActivityOrContext,
{ _: DatePicker, pickedYear: Int, pickedMonth: Int, pickedDay: Int ->
val selectedBirthdate = LocalDate.of(pickedYear, pickedMonth + 1, pickedDay)
val age = Period.between(selectedBirthdate, LocalDate.now()).years
Log.e("DatePickerTag", "Age : $age")
}, initYear, initMonth, initDay)
If I select April 1 1995 and evaluate it against the time of this posting it will print
E/DatePickerTag: Age : 27
If however you can't use java.time or ThreeTenABP, this S.O post might help you otherwise. The bottom section of the post contains answers for calculating age using java.util.Calendar.
Lastly, out-of-topic, consider what lateinit var is for, what kotlin null safety is, and avoid shouting in your code unnecessarily!!
Inside layout file
<DatePicker
android:id="#+id/ageSelectionPicker"
style="#style/MyDatePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="16dp"
android:layout_marginRight="24dp"
android:backgroundTint="#ff4081"
android:calendarTextColor="#ff4081"
android:calendarViewShown="false"
android:datePickerMode="spinner" />
class.java
DatePicker ageSelectionPicker = findViewById(R.id.ageSelectionPicker);
int age = getAge(ageSelectionPicker.getYear(), ageSelectionPicker.getMonth(), ageSelectionPicker.getDayOfMonth());
Method getAge
private int getAge(int year, int month, int day) {
Calendar dateOfBirth = Calendar.getInstance();
Calendar today = Calendar.getInstance();
dateOfBirth.set(year, month, day);
int age = today.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR);
if (today.get(Calendar.DAY_OF_YEAR) < dateOfBirth.get(Calendar.DAY_OF_YEAR)) {
age--;
}
return age;
}
Below is the date picker dialog in one of the fragment and I am getting the error shown below
Type mismatch: inferred type is Int but LocalDate was expected
on line:
viewModel.onDateSelected(year, month, dayOfMonth)
private val datePickerDialog by lazy {
DatePickerDialog(requireActivity(), R.style.DatePicker).apply {
setTitle(R.string.select_date)
datePicker.maxDate = LocalDate.now().minusDays(0).toMillis()
setOnDateSetListener { _, year, month, dayOfMonth ->
viewModel.onDateSelected(year, month, dayOfMonth)
}
}
}
As unowsp pointed out, seems that you need a localDate.
Try something like:
setOnDateSetListener { _, year, month, dayOfMonth ->
val calendar = Calendar.getInstance()
calendar.set(Calendar.YEAR, year)
calendar.set(Calendar.MONTH, month)
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
val localDate = LocalDateTime.ofInstant(calendar.toInstant(), calendar.timeZone.toZoneId())
.toLocalDate()
viewModel.onDateSelected(localDate)
}
So basically i want to get the year from the user using the datePickerDialog and then substract that year to the current year. Till this i have no issues.
The problem is i have button created and want users to get a Toast message if they haven't chosen the date. I am using if else and validating the Year when the datePicker is not selected.
Also the year i am getting after the datePickerDialog is the current year.
Here is the Code -
fun birthdayPicker() {
val cal = Calendar.getInstance()
val year = cal.get(Calendar.YEAR)
val month = cal.get(Calendar.MONTH)
val date = cal.get(Calendar.DATE)
val textcheck : TextView = findViewById(R.id.yourage)
val dateSelected = findViewById<TextView>(R.id.text_view_date_1)
dateSelected.setOnClickListener {
val datePickerDialog = DatePickerDialog(
this,
DatePickerDialog.OnDateSetListener { _, myear, mmonth, mdayOfMonth ->
dateSelected.setText("" + mdayOfMonth + "/" + mmonth + "/" + myear)
// Toast.makeText(this, "$myear", Toast.LENGTH_SHORT).show()
},
year,
month + 1,
date
)
datePickerDialog.show()
}
val button = findViewById<Button>(R.id.button_date_1)
button.setOnClickListener {
val selectedyear : Int = year
if (selectedyear.toString().isBlank()) {
Log.e("Main","$selectedyear")
Toast.makeText(this, "Choose an Year", Toast.LENGTH_SHORT).show()
}
else {
val checkingYear = Calendar.getInstance().get(Calendar.YEAR)
textcheck.text = (checkingYear - selectedyear).toString()
}
}
}
so you just store that into a variable as so
val myFormat = "MM/dd/yyyy" //mention the format you need
val sdf = SimpleDateFormat(myFormat, Locale.US)
val currentDate = sdf.format(cal.getTime())
calendar.setOnClickListener {
datePicker = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view: DatePicker?, year: Int, month: Int, dayOfMonth: Int ->
val curDate = String.format("%d-%02d-%02d", year , (month+1), dayOfMonth)
date_text.setText(curDate)
}, year, month, day)
datePicker!!.datePicker.maxDate = (System.currentTimeMillis() - 1000)
datePicker!!.show()
}
How to select the dob must be a date before today. Please help me to do this
Use it this way :
val calender = getInstance()
calender[HOUR_OF_DAY] = 0
calender[MINUTE] = 0
calender[SECOND] = 0
Then use this in your code
datePicker!!.datePicker.maxDate = calendar.timeInMillis
This way the max date will be set to midnight of today's date.
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
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
}
}