I want the user to be able to select the date of birth using the custom DatePicker from XML, the issue is that I don't know how to get the values selected and store it as a LocalDate variable because the backend stored it as a LocalDate. Firstly I customized it with DatePickerDialog but the thing is so frustrating, just imagine a user that was born in 1990, the person needs to scroll for a while before reaching that and I don't want to use that again as I came across the custom DatePicker which makes me to select the year, month and day differently. Hope you guys understands what I want to achieved. Here is the DatePicker I used in the XML to get the calendar. I want to get all the values selected and store it as a variable of type LocalDate so that I can use it in the user's registration because date of birth is one of the parameters required.
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:spinnersShown="true"/>
This is the one I used before but I don't want to use it again. It's frustrating to scroll down for a longer time if the person that want to register is a bit advanced in age.
this was the XML for it
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="DATE OF BIRTH"
android:textAlignment="center"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Caption"
android:textColor="@color/grey_40" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_date_of_birth"
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="select here"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/grey_40" />
And this was the method in the Java class
private Button btnDateOfBirth;
btnDateOfBirth = findViewById(R.id.btn_date_of_birth);
private void setDatePicker(final Button btnDateOfBirth){
Calendar current_calendar = Calendar.getInstance();
DatePickerDialog dialog = DatePickerDialog.newInstance((view, year, monthOfYear, dayOfMonth) -> {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,monthOfYear);
calendar.set(Calendar.DATE,dayOfMonth);
long date_millis = calendar.getTimeInMillis();
btnDateOfBirth.setText(getFormattedDateEvent(date_millis));
},
current_calendar.get(Calendar.YEAR),
current_calendar.get(Calendar.MONTH),
current_calendar.get(Calendar.DATE));
dialog.setThemeDark(false);
dialog.setAccentColor(getResources().getColor(R.color.cyan_800_overlay));
//dialog.setMinDate(current_calendar);
dialog.show(getFragmentManager(),"Date PickerDialog");
}
public static String getFormattedDateEvent(Long dateTime){
SimpleDateFormat newFormat = new SimpleDateFormat("EEE, yyyy MMM dd");
return newFormat.format(new Date(dateTime));
}
Then used a button to get and store the date selected by setting onclick to it and called the method inside the onclick
btnDateOfBirth.setOnClickListener(view -> {
setDatePicker((Button)view);
});