Wednesday, August 26, 2015

How to set DatePickerDialog cancel button click listener in Android

MainActivity.java

package com.cfsuman.androidtutorials;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import java.util.Calendar;


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        Button button = findViewById(R.id.button);


        // Show the date picker dialog
        button.setOnClickListener(v -> {
            // Initialize a new date picker dialog fragment
            DialogFragment dialogFragment = new DatePickerFragment();

            // Show the date picker dialog fragment
            dialogFragment.show(
                    getSupportFragmentManager(),
                    "Date Picker"
            );
        });
    }



    public static class DatePickerFragment extends DialogFragment
            implements DatePickerDialog.OnDateSetListener{

        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState){
            final Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            // Return the DatePickerDialog
            return new DatePickerDialog(
                    getActivity(),0, this,year,month,day);
        }


        public void onDateSet(
                DatePicker view, int year, int month, int day){
            // Do something with the chosen date
        }


        public void onCancel(@NonNull DialogInterface dialog){
            // Send a message to confirm cancel button click
            Toast.makeText(
                    getActivity(),
                    "Date Picker Dialog Canceled.",
                    Toast.LENGTH_SHORT
            ).show();
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show DatePickerDialog"
        android:textAllCaps="false"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>