created DailyMonthYearPickerDialog
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package `in`.sminnovations.smiadmin.ui.utils
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.app.Dialog
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.widget.CalendarView
|
||||
import android.widget.NumberPicker
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import `in`.sminnovations.smiadmin.R
|
||||
import java.util.Calendar
|
||||
class DailyMonthYearPickerDialog : DialogFragment() {
|
||||
private var onDateSelected: ((Calendar) -> Unit)? = null
|
||||
private var selectedDate: Calendar = Calendar.getInstance()
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
// Inflate the custom layout
|
||||
val view = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_date_month_year_picker, null)
|
||||
|
||||
// Initialize CalendarView
|
||||
val calendarPicker = view.findViewById<CalendarView>(R.id.calendarViewPicker)
|
||||
|
||||
// Set default date
|
||||
calendarPicker.date = selectedDate.timeInMillis
|
||||
|
||||
// Listen for date changes
|
||||
calendarPicker.setOnDateChangeListener { _, year, month, dayOfMonth ->
|
||||
selectedDate.set(Calendar.YEAR, year)
|
||||
selectedDate.set(Calendar.MONTH, month)
|
||||
selectedDate.set(Calendar.DAY_OF_MONTH, dayOfMonth)
|
||||
}
|
||||
|
||||
// Build the Dialog
|
||||
val dialog = AlertDialog.Builder(requireContext())
|
||||
.setView(view)
|
||||
.setTitle("Select Date")
|
||||
.setPositiveButton("OK") { _, _ ->
|
||||
// Pass the selected date back through the callback
|
||||
onDateSelected?.invoke(selectedDate)
|
||||
}
|
||||
.setNegativeButton("Cancel", null)
|
||||
.create()
|
||||
|
||||
return dialog
|
||||
}
|
||||
|
||||
fun setOnDateSelectedListener(listener: (Calendar) -> Unit) {
|
||||
onDateSelected = listener
|
||||
}
|
||||
|
||||
fun setInitialDate(calendar: Calendar) {
|
||||
selectedDate = calendar
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user