2021年5月1日星期六

How to set mindate datepicker in fragment kotlin?

Good Morning bro, i am newbie in android, i want help my friends to set min date in date picker , //for disable yesterday date, this code from my friends , need help bro this file layout to set date picker like this. (example show today in date picker and disable date yesterday). thank you

     <EditText              android:id="@+id/etDates2"              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:layout_margin="16dp"              android:background="@drawable/et_style"              android:padding="12dp"              android:hint="@string/date"              android:drawableStart="@drawable/et_calendar3"              android:drawablePadding="12dp"              android:inputType="date"              android:focusable="false"              android:textStyle="bold"              android:textSize="15.3dp"              android:fontFamily="@font/poppins_regular"              app:layout_constraintEnd_toEndOf="parent"              app:layout_constraintStart_toStartOf="parent"              app:layout_constraintTop_toTopOf="parent" />  

//and datefragment.kt like this below for show datepicker :

import android.app.DatePickerDialog  import android.app.Dialog  import android.os.Bundle  import android.widget.DatePicker  import androidx.fragment.app.DialogFragment  import com.greenhcm.android.common.function.toDateFormat  import com.greenhcm.android.common.function.toTimeMilis  import java.util.*    class DatePickerFragments : DialogFragment(), DatePickerDialog.OnDateSetListener {        private lateinit var action : (String) -> Unit      private var maxDate : String? = null      private var minDate : String? = null        override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {          // Use the current date as the default date in the picker          val c = Calendar.getInstance()          val year = c.get(Calendar.YEAR)          val month = c.get(Calendar.MONTH)          val day = c.get(Calendar.DAY_OF_MONTH)            // Create a new instance of DatePickerDialog and return it          return DatePickerDialog(requireContext(), this, year, month, day).apply {              maxDate?.takeIf { it.isNotEmpty() }?.let {                  datePicker.maxDate = it.toTimeMilis("yyyy-MM-dd")              }              minDate?.takeIf { it.isNotEmpty() }?.let {                  datePicker.minDate = it.toTimeMilis("yyyy-MM-dd")              }          }      }        override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {          action("$year-${month+1}-$day")      }        fun setListener(          maxDate: String? = null,          minDate: String? = null,          action: (String) -> Unit      ) {          this.action = action          this.maxDate = maxDate          this.minDate = minDate      }  }  

// i am confuse here and how to set mindate in datepicker here? (call from another fragments) i stuck here, this code below. i am stuck for set mindate inside setlistener

    etDates2.setOnClickListener {              val newFragment = DatePickerFragment().apply {                  setListener {                      setDate(it)                  }              }              newFragment.show(childFragmentManager, "timePickers")          }  

//parameter setDate in below :

private fun setDate(date: String){      etDates2?.setText(date)  }  
https://stackoverflow.com/questions/67352865/how-to-set-mindate-datepicker-in-fragment-kotlin May 02, 2021 at 11:59AM

没有评论:

发表评论