Skip to content

Using the library

maltaisn edited this page May 30, 2020 · 2 revisions

Here's an example activity that shows a recurrence list dialog on a button click and a recurrence picker fragment if "Custom..." is clicked in the list dialog.

Kotlin
class MainActivity : AppCompatActivity(), RecurrenceListCallback, RecurrencePickerCallback {

    private var selectedRecurrence = Recurrence(Recurrence.Period.NONE)
        set(value) {
            field = value
            Toast.makeText(this, "Recurrence selected: " +
                    settings.formatter.format(this, value, startDate), Toast.LENGTH_SHORT).show()
        }
    
    private val settings = RecurrencePickerSettings()
    private val startDate = System.currentTimeMillis()

    private val listDialog by lazy { RecurrenceListDialog.newInstance(settings) }
    private val pickerFragment by lazy { RecurrencePickerFragment.newInstance(settings) }
    
    override fun onCreate(state: Bundle?) {
        super.onCreate(state)
        setContentView(R.layout.activity_main)
        
        val btn: Button = findViewById(R.id.show_picker_btn)
        btn.setOnClickListener {
            listDialog.selectedRecurrence = selectedRecurrence
            listDialog.startDate = startDate
            listDialog.show(supportFragmentManager, "recurrence-list-dialog")
        }
    }

    override fun onRecurrenceCustomClicked() {
        // The "Custom..." item in the recurrence list dialog was clicked. Show the picker fragment.
        pickerFragment.selectedRecurrence = selectedRecurrence
        pickerFragment.startDate = startDate
        supportFragmentManager.beginTransaction()
                .add(R.id.picker_fragment_container, pickerFragment, "recurrence-picker-fragment")
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                .addToBackStack(null)
                .commit()
    }

    override fun onRecurrencePresetSelected(recurrence: Recurrence) {
        // A recurrence preset item in the recurrence list dialog was selected.
        selectedRecurrence = recurrence
    }

    override fun onRecurrenceCreated(recurrence: Recurrence) {
        // A custom recurrence was created with the recurrence picker fragment.
        selectedRecurrence = recurrence
    }
}
Java
public class MainActivity extends AppCompatActivity
        implements RecurrenceListCallback, RecurrencePickerCallback {

    private Recurrence selectedRecurrence = Recurrence.DOES_NOT_REPEAT;

    private RecurrencePickerSettings settings = new RecurrencePickerSettings.Builder().build();
    private long startDate = System.currentTimeMillis();

    private RecurrenceListDialog listDialog;
    private RecurrencePickerFragment pickerFragment;

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

        listDialog = RecurrenceListDialog.newInstance(settings);
        pickerFragment = RecurrencePickerFragment.newInstance(settings);

        Button btn = findViewById(R.id.show_picker_btn);
        btn.setOnClickListener(v -> {
            listDialog.setSelectedRecurrence(selectedRecurrence);
            listDialog.setStartDate(startDate);
            listDialog.show(getSupportFragmentManager(), "recurrence-list-dialog");
        });
    }

    @Override
    public void onRecurrenceCustomClicked() {
        // The "Custom..." item in the recurrence list dialog was clicked. Show the picker fragment.
        pickerFragment.setSelectedRecurrence(selectedRecurrence);
        pickerFragment.setStartDate(startDate);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.picker_fragment_container, pickerFragment, "recurrence-picker-fragment")
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                .addToBackStack(null)
                .commit();
    }

    @Override
    public void onRecurrencePresetSelected(@NotNull Recurrence recurrence) {
        // A recurrence preset item in the recurrence list dialog was selected.
        setSelectedRecurrence(recurrence);
    }

    @Override
    public void onRecurrenceCreated(@NotNull Recurrence recurrence) {
        // A custom recurrence was created with the recurrence picker fragment.
        setSelectedRecurrence(recurrence);
    }

    private void setSelectedRecurrence(Recurrence recurrence) {
        selectedRecurrence = recurrence;
        Toast.makeText(this, "Recurrence selected: " +
                settings.getFormatter().format(this, recurrence, startDate), Toast.LENGTH_SHORT).show();
    }
}

This code corresponds to the following flow (preset list then picker fragment):

List + fragment demo

A more advanced example can be found in the demo app.

Clone this wiki locally