display Date Picker Dialog - Android User Interface

Android examples for User Interface:Dialog

Description

display Date Picker Dialog

Demo Code


import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class Main{
    private static boolean dialogResult = false;
    private static String dateString = null;
    public static void displayDatePickerDialog(final Context context,
            final View v, int year, int month, int day) {

        /// Date picker dialog = http://www.learn-android-easily.com/2013/06/datepicker-and-timepicker-dialog-in.html
        String title = "Select Date";

        // Register  DatePickerDialog listener
        DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            // the callback received when the user "sets" the Date in the DatePickerDialog
            public void onDateSet(DatePicker view, int yearSelected,
                    int monthOfYear, int dayOfMonth) {

                //  LayoutUtils.displayToast(context, "Setting date");

                // Set the Selected Date in Select date Button
                if (LayoutUtils.getDialogResult()) {
                    LayoutUtils.setDate(yearSelected + "-"
                            + Integer.toString(monthOfYear + 1) + "-"
                            + dayOfMonth);
                    TextView tv = (TextView) v;
                    tv.setText(LayoutUtils.getDate());
                }//from w ww .  j a v a  2  s.co m

            }
        };

        DatePickerDialog dpd = new DatePickerDialog(context,
                mDateSetListener, year, month, day);

        /// Cancel date picker dialog = http://stackoverflow.com/questions/11444238/jelly-bean-datepickerdialog-is-there-a-way-to-cancel
        dpd.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        LayoutUtils.setDialogResult(true);
                        dialog.cancel();

                    }
                });
        dpd.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        LayoutUtils.setDialogResult(false);
                        dialog.cancel();
                    }
                });

        dpd.show();

    }
    public static boolean getDialogResult() {
        return dialogResult;
    }
    public static void setDate(String date) {
        dateString = date;
    }
    public static String getDate() {
        return dateString;
    }
    public static void setDialogResult(boolean result) {
        dialogResult = result;
    }
}

Related Tutorials