I have a question. I created a DatePickerDialog in Android Studio, but when the Datepickerdialog is displayed and I want to add a date, it doesn't work. I would like to see the DialogPicker when I press the button and thus change the date, but nothing appears in the text view. My code is down here. It would also be nice if someone told me how to make the date in the order of month, day, year
Fragmentclass:
public class Fragment extends Fragment implements DatePickerDialog.OnDateSetListener {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
calendar = Calendar.getInstance();
Button button = (Button) getView().findViewById(R.id.change);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datepicker = new DatePickerFragment();
datepicker.show(getFragmentManager(), "date picker");
}
});
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String currentime = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());
TextView textView = getView().findViewById(R.id.date);
textView.setText(currentime);
}
}
Class of DatePicker: public class DatePickerFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(),(DatePickerDialog.OnDateSetListener) getTargetFragment(),
year, month, day);
}
}