Example usage for android.app DatePickerDialog setOnKeyListener

List of usage examples for android.app DatePickerDialog setOnKeyListener

Introduction

In this page you can find the example usage for android.app DatePickerDialog setOnKeyListener.

Prototype

public void setOnKeyListener(@Nullable OnKeyListener onKeyListener) 

Source Link

Document

Sets the callback that will be called if a key is dispatched to the dialog.

Usage

From source file:com.simplaapliko.trips.presentation.fragment.SelectDateDialog.java

@NonNull
@Override/*from  ww  w.ja va2s  .c  o m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final DatePickerDialog pickerDialog = new DatePickerDialog(getActivity(), this,
            mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DATE));

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        pickerDialog.getDatePicker().setCalendarViewShown(false);
    }

    pickerDialog.setOnKeyListener((dialog, keyCode, event) -> {
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            dialog.cancel();
            dialog.dismiss();
            return true;
        default:
            return false;
        }
    });

    return pickerDialog;
}