Using the dialog picker

maltaisn edited this page May 19, 2018 · 5 revisions

Here's an example activity that shows a recurrence picker dialog on a button click.

public class MainActivity extends AppCompatActivity implements RecurrencePickerDialog.RecurrenceSelectedCallback {

    private Recurrence recurrence;
    private DateFormat dateFormatShort;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create date formats
        final DateFormat dateFormatLong = new SimpleDateFormat("EEE MMM dd, yyyy", Locale.ENGLISH);  // Sun Dec 31, 2017
        dateFormatShort = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);  // 31-12-2017

        final RecurrencePickerDialog picker = new RecurrencePickerDialog();
        picker.setDateFormat(dateFormatShort, dateFormatLong);

        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Open recurrence picker dialog
                picker.setRecurrence(recurrence, System.currentTimeMillis());
                picker.show(getFragmentManager(), "recur_picker");
            }
        });

    }

    @Override
    public void onRecurrenceSelected(Recurrence r) {
        // Called when recurrence picker dialog creates a recurrence
        recurrence = r;
        Toast.makeText(this, "Recurrence selected: " +
                r.format(this, dateFormatShort), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCancelled(Recurrence r) {
        // Called when cancel button is clicked (if shown) or if dialog is cancelled by click outside
        Toast.makeText(this, "Picker dialog cancelled", Toast.LENGTH_SHORT).show();
    }
}

It is important to set date formats, otherwise the picker will use system's defaults, which are not necessarily the best.

To show the picker, you must use these two methods:

  • picker.setRecurrence(Recurrence r, long startDate): sets a recurrence to show in the dialog. The dialog does not save last selected recurrence, you must do it yourself by saving it in onRecurrenceSelected callback, or use getPickerView().getRecurrence(). (I have not tested this, it should work)
  • picker.show(FragmentManager fm, String tag): shows the DialogFragment

If you wish to get the RecurrencePickerView view that the dialog uses, you can call getPickerView(). Note that the view changes everytime the dialog is shown and on configuration changes, do not keep the old instance in memory, that's a memory leak!

To use the dialog with a fragment, you must call setTargetFragment(Fragment) on the dialog to be able to receive the callbacks.

Screenshots

Monthly recurrence
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.