Example usage for android.app DatePickerDialog DatePickerDialog

List of usage examples for android.app DatePickerDialog DatePickerDialog

Introduction

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

Prototype

public DatePickerDialog(@NonNull Context context, @Nullable OnDateSetListener listener, int year, int month,
        int dayOfMonth) 

Source Link

Document

Creates a new date picker dialog for the specified date using the parent context's default date picker dialog theme.

Usage

From source file:com.activiti.android.ui.fragments.form.picker.DatePickerFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    if (getArguments() != null) {
        if (getArguments().containsKey(ARGUMENT_TIME_PICKER)) {
            showTime = getArguments().getBoolean(ARGUMENT_TIME_PICKER);
        }//from ww  w. ja  v  a2s .c om
        if (getArguments().containsKey(ARGUMENT_MIN_DATE)) {
            minDate = getArguments().getLong(ARGUMENT_MIN_DATE);
        }
        if (getArguments().containsKey(ARGUMENT_MAX_DATE)) {
            maxDate = getArguments().getLong(ARGUMENT_MAX_DATE);
        }
        if (getArguments().containsKey(ARGUMENT_START_DATE)) {
            startDate = getArguments().getLong(ARGUMENT_START_DATE);
        }
    }

    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    if (startDate != null) {
        c.setTime(new Date(startDate));
    }
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    picker = new DatePickerDialog(getActivity(), mListener, year, month, day);

    if (maxDate != null) {
        picker.getDatePicker().setMaxDate(maxDate);
    }

    if (minDate != null) {
        picker.getDatePicker().setMinDate(minDate);
    }

    picker.setButton(DialogInterface.BUTTON_POSITIVE, getActivity().getString(android.R.string.ok),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    picker.onClick(dialog, which);
                    dismiss();
                }
            });
    picker.setButton(DialogInterface.BUTTON_NEUTRAL, getActivity().getString(R.string.clear),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    clearValue = true;
                    onDateSet(picker.getDatePicker(), picker.getDatePicker().getYear(),
                            picker.getDatePicker().getMonth(), picker.getDatePicker().getDayOfMonth());
                    dismiss();
                }
            });
    picker.setButton(DialogInterface.BUTTON_NEGATIVE, getActivity().getString(R.string.general_action_cancel),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    isCancelled = true;
                    onDateSet(picker.getDatePicker(), picker.getDatePicker().getYear(),
                            picker.getDatePicker().getMonth(), picker.getDatePicker().getDayOfMonth());
                    dismiss();
                }
            });
    return picker;

}

From source file:nl.inversion.carexpense.AddCar_Activity.java

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 1:/*from  w w w  .j a  va 2s .c om*/
        // set date picker as current date
        return new DatePickerDialog(this, purDatePickerListener, year, month, day);
    case 2:
        // set date picker as current date
        return new DatePickerDialog(this, MOTDatePickerListener, year, month, day);
    case 3:
        // set date picker as current date
        return new DatePickerDialog(this, buildDatePickerListener, year, month, day);
    }
    return null;
}

From source file:com.devgmail.mitroshin.totutu.controllers.StartFragment.java

private void setDateField() {

    Calendar myCalendar = Calendar.getInstance();

    mDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
        @Override/*from   w  w  w.j a  v  a  2s. com*/
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            Calendar myDate = Calendar.getInstance();
            myDate.set(year, month, dayOfMonth);
            datePickerButton.setText(mSimpleDateFormat.format(myDate.getTime()));
        }
    }, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
}

From source file:org.akvo.caddisfly.sensor.colorimetry.liquid.SaveCalibrationDialogFragment.java

@NonNull
@Override/*from w  ww .j  a  v  a 2 s.  c o  m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {

    String testCode = CaddisflyApp.getApp().getCurrentTestInfo().getId();
    final Context context = getActivity();

    LayoutInflater i = getActivity().getLayoutInflater();

    @SuppressLint("InflateParams")
    View view = i.inflate(R.layout.fragment_save_calibration, null);

    final DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, monthOfYear);
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            String date = new SimpleDateFormat("dd MMM yyyy", Locale.US).format(calendar.getTime());
            editExpiryDate.setText(date);
        }
    };

    editExpiryDate = (EditText) view.findViewById(R.id.editExpiryDate);

    editBatchCode = (EditText) view.findViewById(R.id.editBatchCode);

    editRgb = (EditText) view.findViewById(R.id.editRgb);

    long milliseconds = PreferencesUtil.getLong(getActivity(),
            CaddisflyApp.getApp().getCurrentTestInfo().getId(), R.string.calibrationExpiryDateKey);
    if (milliseconds > new Date().getTime()) {

        editBatchCode.setText(PreferencesUtil.getString(context, testCode, R.string.batchNumberKey, "").trim());

        long expiryDate = PreferencesUtil.getLong(getContext(), testCode, R.string.calibrationExpiryDateKey);
        if (expiryDate >= 0) {
            calendar.setTimeInMillis(expiryDate);

            editExpiryDate.setText(new SimpleDateFormat("dd-MMM-yyyy", Locale.US).format(new Date(expiryDate)));
        }
    }

    editRgb.setText(PreferencesUtil.getString(context, testCode, R.string.ledRgbKey, "").trim());

    if (!AppPreferences.useExternalCamera()) {
        editRgb.setVisibility(View.GONE);
        view.findViewById(R.id.textRgb).setVisibility(View.GONE);
    }

    final DatePickerDialog datePickerDialog = new DatePickerDialog(context, onDateSetListener,
            calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

    Calendar date = Calendar.getInstance();
    date.add(Calendar.DATE, 1);
    date.set(Calendar.HOUR_OF_DAY, date.getMinimum(Calendar.HOUR_OF_DAY));
    date.set(Calendar.MINUTE, date.getMinimum(Calendar.MINUTE));
    date.set(Calendar.SECOND, date.getMinimum(Calendar.SECOND));
    date.set(Calendar.MILLISECOND, date.getMinimum(Calendar.MILLISECOND));
    datePickerDialog.getDatePicker().setMinDate(date.getTimeInMillis());
    date.add(Calendar.MONTH, CaddisflyApp.getApp().getCurrentTestInfo().getMonthsValid());
    date.set(Calendar.HOUR_OF_DAY, date.getMaximum(Calendar.HOUR_OF_DAY));
    date.set(Calendar.MINUTE, date.getMaximum(Calendar.MINUTE));
    date.set(Calendar.SECOND, date.getMaximum(Calendar.SECOND));
    date.set(Calendar.MILLISECOND, date.getMaximum(Calendar.MILLISECOND));
    datePickerDialog.getDatePicker().setMaxDate(date.getTimeInMillis());

    editExpiryDate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (b) {
                closeKeyboard(getContext(), editBatchCode);
                datePickerDialog.show();
            }
        }
    });

    editExpiryDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            closeKeyboard(getContext(), editBatchCode);
            datePickerDialog.show();
        }
    });

    editName = (EditText) view.findViewById(R.id.editName);
    if (!isEditing && AppPreferences.isDiagnosticMode()) {
        editName.requestFocus();
    } else {
        editName.setVisibility(View.GONE);
        editBatchCode.requestFocus();
    }

    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

    AlertDialog.Builder b = new AlertDialog.Builder(getActivity()).setTitle(R.string.calibrationDetails)
            .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    closeKeyboard(context, editName);
                    dismiss();
                }
            });

    b.setView(view);
    return b.create();
}

From source file:org.linesofcode.alltrack.graph.AddValueActivity.java

private void showDatePicker() {
    new DatePickerDialog(this, this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH)).show();
}

From source file:net.evecom.android.log.DailyLogLookActivity.java

/**
 * /*from   ww  w .j  av a2s  .c  o m*/
 */
private void listener() {

    gzsjButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Dialog alertdialog = new AlertDialog.Builder(DailyLogLookActivity.this).setTitle("")
                    .setPositiveButton("", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Dialog dialog1 = new DatePickerDialog(DailyLogLookActivity.this,
                                    new DatePickerDialog.OnDateSetListener() {

                                        public void onDateSet(DatePicker view, int year, int monthOfYear,
                                                int dayOfMonth) {
                                            String MonthOfYear = "";
                                            String DayOfMonth = "";
                                            years = year;
                                            month = monthOfYear + 1;
                                            day = dayOfMonth;
                                            if (monthOfYear < 9) {

                                                MonthOfYear = "0" + month;
                                            } else {
                                                MonthOfYear = "" + month;
                                            }
                                            if (dayOfMonth < 9) {
                                                DayOfMonth = "0" + dayOfMonth;
                                            } else {
                                                DayOfMonth = "" + dayOfMonth;
                                            }
                                            gzsjButton
                                                    .setText("" + year + "-" + MonthOfYear + "-" + DayOfMonth);

                                            gzsj = years + "-" + month + "-" + day;
                                        }
                                    }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                                    calendar.get(Calendar.DAY_OF_MONTH));// 
                            dialog1.show();
                        }
                    })
                    // .setNeutralButton("", new
                    // DialogInterface.OnClickListener() {
                    // @Override
                    // public void onClick(DialogInterface dialog,int which)
                    // {
                    // Dialog dialog2 = new TimePickerDialog(
                    // DailyLogLookActivity.this,
                    // new TimePickerDialog.OnTimeSetListener() {
                    // public void onTimeSet(TimePicker view, int hour, int
                    // minute) {
                    // gzsjButton.setText("" + years + "-"
                    // + month
                    // + "-" + day +" "
                    // + hour +":"
                    // + minute+":"
                    // + calendar.get(Calendar.SECOND));
                    // hours= hour;
                    // minutes = minute;
                    // gzsj= years + "-"+ month + "-" + day
                    // +"+" + hours + "%3A" + minutes + "%3A" +
                    // calendar.get(Calendar.SECOND);
                    // }
                    // }, calendar.get(Calendar.HOUR_OF_DAY), calendar
                    // .get(Calendar.MINUTE), true);// 
                    // dialog2.show();
                    // }
                    // })
                    .setNegativeButton("", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            gzsjButton.setText("");

                            gzsj = "";

                        }
                    }).show();
        }
    });
    // 
    rzlxButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Dialog schDia = new AlertDialog.Builder(DailyLogLookActivity.this)
                    .setIcon(R.drawable.login_error_icon).setTitle("")
                    .setItems(rzlxs, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dia, int which) {
                            rzlxButton.setText(rzlxs[which]);
                            sourceway = which + 1;
                        }
                    }).setNegativeButton("", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dia, int which) {
                            dia.dismiss();
                        }
                    }).create();
            schDia.show();
        }
    });
}

From source file:jp.co.noxi.app.NXDatePickerDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    if (!mInitFlag) {
        throw new RuntimeException("Use NXDatePickerDialog#newInstance");
    }//from   w  w w.j  a  va 2s . c o m

    final Bundle args = getArguments();
    final int theme = args.getInt(ARG_THEME);
    final int year = args.getInt(ARG_YEAR);
    final int month = args.getInt(ARG_MONTH);
    final int day = args.getInt(ARG_DAY);

    if (theme == VALUE_NULL) {
        return mDialog = new DatePickerDialog(getActivity(), this, year, month, day);
    } else {
        return mDialog = new DatePickerDialog(getActivity(), theme, this, year, month, day);
    }
}

From source file:de.kirchnerei.bicycle.battery.BatteryEditFragment.java

private void doPickBatteryDate() {

    final Calendar now = Calendar.getInstance();
    now.setTime(item.getDate());//from www . java2  s  . com

    DatePickerDialog dlg = new DatePickerDialog(getActivity(),
            // TODO Define a Theme for the DatePickerDialog
            new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    synchronized (now) {
                        now.set(Calendar.YEAR, year);
                        now.set(Calendar.MONTH, monthOfYear);
                        now.set(Calendar.DATE, dayOfMonth);
                        updateDate(now.getTime());
                    }
                }
            }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE));
    dlg.getDatePicker().setMaxDate(System.currentTimeMillis());
    dlg.getDatePicker().setFirstDayOfWeek(Calendar.MONDAY);
    dlg.show();
}

From source file:de.nico.ha_manager.activities.AddHomework.java

public void setUntil(View v) {
    DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

        @Override//from  w  w w  .j a  va  2 s. c o m
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            date[0] = year;
            date[1] = monthOfYear;
            date[2] = dayOfMonth;
            setTextViewUntil(date);

        }

    }, date[0], date[1], date[2]);

    dpd.show();
}

From source file:cn.count.easydrive366.CarRegistrationEditActivity.java

private void chooseDate() {
    String d = ((TextView) findViewById(R.id.edt_carregistration_registration_date)).getText().toString();
    if (d.equals("")) {
        d = "2000-01-01";
    }//  ww w  .ja  v a 2s . c om
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    final Calendar c = Calendar.getInstance();

    try {
        c.setTime(sdf.parse(d));

        Dialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                c.set(Calendar.YEAR, year);
                c.set(Calendar.MONTH, monthOfYear);
                c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                //showDialog(sdf.format(c.getTime()));
                ((TextView) findViewById(R.id.edt_carregistration_registration_date))
                        .setText(sdf.format(c.getTime()));
            }
        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    } catch (Exception e) {
        log(e);
    }

}