New to android, so I'm not sure if I am using the correct terminology. I have a date and time picker, whose entries I am trying to save into a single calendar instance, but the time and date picked is not being saved into the calendar, rather it is the current time and date. I'm not sure what I am doing wrong.
class AddReminderActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_reminder)
val timePicker: TimePicker = findViewById(R.id.reminder_time)
val datePicker: DatePicker = findViewById(R.id.reminder_date)
val combinedCal = GregorianCalendar(TimeZone.getTimeZone("PDT"))
combinedCal.set(datePicker.year, datePicker.month, datePicker.dayOfMonth, timePicker.hour, timePicker.minute)
val button: Button = findViewById(R.id.reminder_save_button)
button.setOnClickListener{
Toast.makeText(it.context, combinedCal.time.toString(), Toast.LENGTH_LONG).show()
}
}
}
CodePudding user response:
Instead of this:
val combinedCal = GregorianCalendar(TimeZone.getTimeZone("PDT"))
combinedCal.set(datePicker.year, datePicker.month, datePicker.dayOfMonth, timePicker.hour, timePicker.minute)
val button: Button = findViewById(R.id.reminder_save_button)
button.setOnClickListener{
Toast.makeText(it.context, combinedCal.time.toString(), Toast.LENGTH_LONG).show()
}
Try doing this:
val button: Button = findViewById(R.id.reminder_save_button)
button.setOnClickListener{
val combinedCal = GregorianCalendar(TimeZone.getTimeZone("PDT"))
combinedCal.set(datePicker.year, datePicker.month, datePicker.dayOfMonth, timePicker.hour, timePicker.minute)
Toast.makeText(it.context, combinedCal.time.toString(), Toast.LENGTH_LONG).show()
}
CodePudding user response:
Use DatePicker.setOnDateChangedListener() to listen to the changes and change your calendar value based on that.
Since 26 API, we can set listener like that DatePicker.setOnDateChangedListener(). But for versions before that, we need to initialize it with the date we want to display at first. Here we can add the listener as well.
So we are first checking the Build Version and writing two different code for different versions of android.
Your code should look like this-
class AddReminderActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_reminder)
val timePicker: TimePicker = findViewById(R.id.reminder_time)
val datePicker: DatePicker = findViewById(R.id.reminder_date)
val combinedCal = GregorianCalendar(TimeZone.getTimeZone("PDT"))
combinedCal.set(datePicker.year, datePicker.month, datePicker.dayOfMonth, timePicker.hour, timePicker.minute)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
dp_picker.setOnDateChangedListener { _, year, monthOfYear, dayOfMonth ->
combinedCal.set(
year,
monthOfYear,
dayOfMonth
)
}
time_picker.setOnTimeChangedListener { _, hourOfDay, minute ->
combinedCal.time.hours = hourOfDay
combinedCal.time.minutes = minute
}
} else {
dp_picker.init(
combinedCal.get(Calendar.YEAR),
combinedCal.get(Calendar.MONTH),
combinedCal.get(Calendar.DAY_OF_MONTH)
) { _, year, monthOfYear, dayOfMonth ->
combinedCal.set(
year,
monthOfYear,
dayOfMonth
)
}
time_picker.setOnTimeChangedListener { _, hourOfDay, minute ->
combinedCal.time.hours = hourOfDay
combinedCal.time.minutes = minute
}
}
val button: Button = findViewById(R.id.reminder_save_button)
button.setOnClickListener{
Toast.makeText(it.context, combinedCal.time.toString(), Toast.LENGTH_LONG).show()
}
}
