1

I am trying to change the text of a button to the chosen date to further store in the firebase database. Heres the java code I used:

public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        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);
        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }
    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user   
    }
}
public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new MortgageProtectionActivity.DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
}

The ID of the button is btn_pf_dob and showDatePickerDialog for onclick.

Nisath
  • 55
  • 1
  • 8

1 Answers1

1

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);
                }
            }
            );
        }
    }
Tyler V
  • 9,694
  • 3
  • 26
  • 52
  • I am a beginner so i struggle a bit. I replaced my showDatePickerDialog with your override method and changed the 'yourMethodhere' to 'DatePickerDialog'. it highlights all the override method is red claiming 'Fragments should be static such that they can be reinstated'. tried adding static between public and void but it didnt really work. – Nisath Jul 15 '18 at 20:01
  • Good point. Take a look here https://stackoverflow.com/questions/11527051/get-date-from-datepicker-using-dialogfragment – Tyler V Jul 15 '18 at 20:07
  • I added an alternate approach in the answer too (it's worked well for me in the past) – Tyler V Jul 15 '18 at 20:12