I'm trying to change the date picker style to spinner for one date picker, others shall remain default.
I've tried:
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
private int style;
DatePickerFragment(int xml) {
style = xml;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
final DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
LayoutInflater inflater = requireActivity().getLayoutInflater();
datePickerDialog.setView(inflater.inflate(style, null));
return datePickerDialog;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Log.i("DatePicker", "received date from picker: " + day + "." + month + "." + year);
}
}
with style XML:
<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:calendarViewShown="false" />
but whenever I enter a date in the spinner and hit OK, the current (todays) date is logged in the onDateSet method and not the chosen one in the date picker.
I also tried to overwrite the OK button with:
datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "asd", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.i("DatePicker", "received date from picker2: " + datePickerDialog.getDatePicker().getDayOfMonth() + "." + datePickerDialog.getDatePicker().getMonth()+1 + "." + datePickerDialog.getDatePicker().getYear());
}
});
but without change.
From my understanding and the behavior, I'm creating some overlay with inflater.inflate(style, null) where I select the date, but the overlay values are not populated.
How can I use the inflater to change the date picker style?
Note...I can't:
- change the style since I also need "regular" date pickers.
- Use a new Activity, I want to stick with fragments.
Thanks!