You could override the onDateSet
method to put a value on the button or elsewhere. To do this with a dialog fragment, you can have your main activity implement OnDateSetListener
rather than the dialog (see this question or this one). Alternately, see Edit 2 below.
EDIT 1: If you don't need to use a DialogFragment
, another way of doing this would be
public void showDatePickerDialog(View v) {
final Button btn = findViewById(R.id.btn_pf_dob);
final DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// generate string from year, month, and day
String dateStr = yourMethodHere(year,month,day);
btn.setText(dateStr);
}
};
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(this, dateSetListener, year, month, day);
dpd.show();
}
EDIT 2: If you need to use a DialogFragment
but don't want to have to make your Activity implement onDateSet
, you could use something like the following. Why might you want this? If you have multiple date pickers launched from the same activity you may want different listeners for each one. Implementing it on the Activity prevents that, while this approach lets you make a unique listener for each date picker.
public static class DatePickerFragment extends DialogFragment {
private DatePickerDialog.OnDateSetListener listener = null;
void setListener(DatePickerDialog.OnDateSetListenerlistener) {
this.listener = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
DatePickerDialog dpd = null;
if( listener != null ) {
// Use the current date as the default date in the picker
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog
dpd = new DatePickerDialog(getActivity(), listener, year, month, day);
}
return dpd;
}
}
public void showDatePickerDialog(View v) {
final Button btn = findViewById(R.id.btn_pf_dob);
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setListener(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
String dateStr = "date: " + year + " " + month + " " + day;
btn.setText(dateStr);
}
}
);
newFragment.show(getFragmentManager(), "datePicker");
}
To set this up properly in the Activity, with screen rotation, you will need to reset the listener in your Activity onCreate
for when the screen is rotated while the dialog is shown, with something like
if (savedInstanceState != null) {
DatePickerDialogFragment dpf = (DatePickerDialogFragment) getFragmentManager()
.findFragmentByTag("datePicker");
if (dpf != null) {
dpf.setListener(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
String dateStr = "date: " + year + " " + month + " " + day;
btn.setText(dateStr);
}
}
);
}
}