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)
}
Related
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
}
}
how can set max date in date picker Kotlin android
how can set current date in max date so user cannot enter future date
val date =
OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
myCalendar.set(Calendar.YEAR, year)
myCalendar.set(Calendar.MONTH, monthOfYear)
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
dob.set(year,monthOfYear,dayOfMonth)
var age = today[Calendar.YEAR] - dob[Calendar.YEAR]
if (today[Calendar.DAY_OF_YEAR] < dob[Calendar.DAY_OF_YEAR]) {
age--
}
val ageInt = age
val ageS = ageInt.toString()
("Age : $ageS").also { binding.ageText.text = it }
updateLabel()
}
binding.edtBirthdate.setOnClickListener {
context?.let { it1 ->
DatePickerDialog(
it1, date, myCalendar[Calendar.YEAR],
myCalendar[Calendar.MONTH],
myCalendar[Calendar.DAY_OF_MONTH]
)
.show()
}
}
In Kotlin, you can do something like
DatePickerDialog(
it1, date, myCalendar[Calendar.YEAR],
myCalendar[Calendar.MONTH],
myCalendar[Calendar.DAY_OF_MONTH]
)
.apply { datePicker.maxDate = Date().time }
.show()
I am using DatePickerDialog in my Android project using Kotlin. I have successfully created DatePickerDialog and its working fine.
Now I want to give some validation like in DatePickerDialog I want to disable future date.
For Example, Today date is 22 Aug 2019 I want to disable all date after today date.
I tried to use dateSetListener.getDatePicker().setMaxDate(System.currentTimeMillis())
but it's not working
var cal = Calendar.getInstance()
// 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()
}
// dateSetListener.getDatePicker().setMaxDate(calendar.getTimeInMillis());
}
// 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()
}
})```
Try this
var dialog = 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)
)
dialog.datePicker.maxDate = Calendar.getInstance().timeInMillis
dialog.show()
I want to get date in this format "Saturday, 6/10/2018" from the Date Picker Dialog. The problem is that when I select any date from current month it displays true output but when I select any date from previous or next month it displays wrong name of day of the week.
Note: Previously asked questions doesn't helped me...
Here is my code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="#+id/pickDateBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="Pick Full Date" />
<TextView
android:id="#+id/displayDateTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Full Date will display here..."
android:textColor="#000"
android:textSize="20sp" />
</LinearLayout>
MainActivity.kt
package com.blogspot.atifsoftwares.fulldateformat
import android.app.DatePickerDialog
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import java.text.SimpleDateFormat
import java.util.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
pickDateBtn.setOnClickListener {
pickDate()
}
}
private fun pickDate() {
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 dpd = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view, mYear, monthOfYear, dayOfMonth ->
val simpleDateFormat = SimpleDateFormat("EEEE")
val date = Date(mYear, month, dayOfMonth - 1)
val dayString = simpleDateFormat.format(date) //returns true day name for current month only
displayDateTv.text = "$dayString, $dayOfMonth/${monthOfYear + 1}/$mYear"
}, year, month, day)
dpd.show()
}
}
val dpd = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view, mYear, monthOfYear, dayOfMonth ->
val simpleDateFormat = SimpleDateFormat("EEEE")
val date = Date(mYear, month, dayOfMonth - 1)
val dayString = simpleDateFormat.format(date) //returns true day name for current month only
displayDateTv.text = "$dayString, $dayOfMonth/${monthOfYear + 1}/$mYear"
}, year, month, day)
Change
val date = Date(mYear, month, dayOfMonth - 1)
to val date = Date(mYear, monthOfYear, dayOfMonth - 1)
Explaination : you are using closure month variable not the one you got in lambda while initializing Date class.
val dpd = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view, mYear, monthOfYear, dayOfMonth ->
val simpleDateFormat = SimpleDateFormat("EEEE")
val date = Date(mYear, monthOfYear, dayOfMonth - 1)
val dayString = simpleDateFormat.format(date) //returns true day name for current month only
displayDateTv.text = "$dayString, $dayOfMonth/${monthOfYear + 1}/$mYear"
}, year, month, day)
You were using closure month variable not the one you got in lambda while initializing Date class. Check this edited code.
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
}
}