I need to make the minDate from 100 ago,because it is use for set the birthday . I had the code below,but it is from now to 100 years after .How to modify it ?
I have tried different ways ,such as add -100 years ,but not working .. And the most resolves I found are Java ..I will need with Kotlin
Thank you so much in advance !
class ThirdActivity : AppCompatActivity() {
private var year = 0
private var month = 0
private var day = 0
lateinit var edt :EditText
private lateinit var calendar: Calendar
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_third)
edt = findViewById(R.id.edt_birthday)
edt.setOnClickListener {
calendar = Calendar.getInstance()
year = calendar.get(Calendar.YEAR)
month = calendar.get(Calendar.MONTH)
day = calendar.get(Calendar.DAY_OF_MONTH)
val dialog = DatePickerDialog(this,AlertDialog.THEME_HOLO_LIGHT,{_,year,month,day_of_month->
calendar[Calendar.YEAR] = year
calendar[Calendar.MONTH] =month 1
calendar[Calendar.DAY_OF_MONTH] = day_of_month
val myFormat = "dd/MM/yyyy"
val sdf = SimpleDateFormat(myFormat,Locale.getDefault())
edt.setText(sdf.format(calendar.time))
},calendar[Calendar.YEAR], calendar[Calendar.MONTH], calendar[Calendar.DAY_OF_MONTH])
dialog.datePicker.minDate = calendar.timeInMillis
dialog.datePicker.maxDate =calendar.timeInMillis
calendar.add(Calendar.YEAR, 100)
dialog.datePicker.maxDate = calendar.timeInMillis
dialog.show()
}
}
}
activity_third.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".ThirdActivity">
<EditText
android:id="@ id/edt_birthday"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="380dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:hint="Choose your birthday"
android:inputType="text"
android:textColor="@color/black"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
CodePudding user response:
edt.setOnClickListener {
calendar = Calendar.getInstance()
// create yearCal instance and -100 years
val yearCal = Calendar.getInstance()
yearCal.add(Calendar.YEAR, -100)
year = calendar.get(Calendar.YEAR)
month = calendar.get(Calendar.MONTH)
day = calendar.get(Calendar.DAY_OF_MONTH)
val dialog = DatePickerDialog(this,
AlertDialog.THEME_HOLO_LIGHT,{ _, year, month, day_of_month->
calendar[Calendar.YEAR] = year
calendar[Calendar.MONTH] =month 1
calendar[Calendar.DAY_OF_MONTH] = day_of_month
val myFormat = "dd/MM/yyyy"
val sdf = SimpleDateFormat(myFormat,Locale.getDefault())
edt.setText(sdf.format(calendar.time))
},calendar[Calendar.YEAR], calendar[Calendar.MONTH], calendar[Calendar.DAY_OF_MONTH])
//set minDate from yearCall
dialog.datePicker.minDate = yearCal.timeInMillis
dialog.datePicker.maxDate = calendar.timeInMillis
dialog.show()
}