I'm trying to make a DatePickerDialog to change a TextView. But I also want to display a Toast when the user click on the "Ok" button.
So, I implemented DatePickerDialog.OnClickListener
and DatePickerDialog.OnDateSetListener
and I override both method. But nothing happen when I click the "Ok" button on the DatePicker. Any idea ?
Thank in advance, here is my DatePickerFragment :
@RequiresApi(api = Build.VERSION_CODES.N)
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener, DatePickerDialog.OnClickListener {
public static int year_x;
public static int month_x;
public static int day_x;
public static Calendar calendar = Calendar.getInstance(Locale.FRANCE);
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance(Locale.FRANCE);
final int year = c.get(Calendar.YEAR);
final int month = c.get(Calendar.MONTH);
final int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
/*
// When I do that way and not implement DatePickerDialog.OnClickListener the date is not updated in my TextView switcherDateDay
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() {
@SuppressLint("ShowToast")
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Test", Toast.LENGTH_LONG).show();
}
});
*/
return dialog;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == DialogInterface.BUTTON_POSITIVE) {
Toast.makeText(getActivity(), "Date is selected", Toast.LENGTH_LONG).show();
}
}
@TargetApi(Build.VERSION_CODES.N)
@RequiresApi(api = Build.VERSION_CODES.M)
public void onDateSet(DatePicker view, int year, int month, int day) {
year_x = view.getYear();
month_x = view.getMonth();
day_x = view.getDayOfMonth();
calendar.set(year_x, month_x, day_x);
ScheduleCalendar scheduleCal = new ScheduleCalendar(calendar, 0);
TextView switcherDateDay = (TextView) getActivity().findViewById(R.id.schedule_switcher_date);
TextView switcherCurrentDay = (TextView) getActivity().findViewById(R.id.schedule_switcher_current_day);
switcherDateDay.setText(String.format("%s %s %s", scheduleCal.getDayName(), scheduleCal.getDayNo(), scheduleCal.getMonthName()));
switcherCurrentDay.setText(ScheduleSwitcherFragment.statueOfWeek(scheduleCal));
}
}
Show your Toast in onDateSet method of datepicker dialog