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.kubotaku.android.sample.sensordataviewer.fragments.DateTimePickerFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    loadTime();//from   w  ww. jav a2s.  c o  m

    if (type == TYPE_PICKER_DATE) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        c.setTime(this.selectedDate);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    } else {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        c.setTime(this.selectedDate);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }
}

From source file:com.androidformenhancer.internal.impl.DatePickerDialogSupportFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Bundle args = getArguments();/*from   w  ww. j ava 2s .  c  o  m*/
    int id = args.getInt(ARG_TARGET_VIEW_RES_ID);
    View v = getActivity().findViewById(id);
    if (v == null || !(v instanceof TextView)) {
        throw new IllegalArgumentException("Target view must be valid TextView: " + v);
    }
    final TextView tv = (TextView) v;
    if (TextUtils.isEmpty(tv.getText())) {
        int defaultMessageId = args.getInt(ARG_DEFAULT_MESSAGE_RES_ID);
        tv.setText(getActivity().getString(defaultMessageId));
    }

    DatePickerDialog.OnDateSetListener callBack = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar c = Calendar.getInstance(Locale.getDefault());
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, monthOfYear);
            c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            tv.setText(DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()).format(c.getTime()));
        }
    };
    Calendar c = Calendar.getInstance();
    final Dialog dialog = new DatePickerDialog(getActivity(), callBack, c.get(Calendar.YEAR),
            c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)) {
    };
    return dialog;
}

From source file:com.example.reminder.alarm.DatePickerFragment.java

@TargetApi(11)
@Override/*from w  ww .ja v a  2s  . c  o  m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Bundle b = getArguments();
    int y = b.getInt(YEAR);
    int m = b.getInt(MONTH);
    int d = b.getInt(DATE);

    // Jelly Bean introduced a bug in DatePickerDialog (and possibly 
    // TimePickerDialog as well), and one of the possible solutions is 
    // to postpone the creation of both the listener and the BUTTON_* .
    // 
    // Passing a null here won't harm because DatePickerDialog checks for a null
    // whenever it reads the listener that was passed here. >>> This seems to be 
    // true down to 1.5 / API 3, up to 4.1.1 / API 16. <<< No worries. For now.
    //
    // See my own question and answer, and details I included for the issue:
    //
    // http://stackoverflow.com/a/11493752/489607
    // http://code.google.com/p/android/issues/detail?id=34833
    //
    // Of course, suggestions welcome.

    final DatePickerDialog picker = new DatePickerDialog(getActivity(), getConstructorListener(), y, m, d);

    if (hasJellyBeanAndAbove()) {
        picker.setButton(DialogInterface.BUTTON_POSITIVE, getActivity().getString(android.R.string.ok),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DatePicker dp = picker.getDatePicker();
                        mListener.onDateSet(dp, dp.getYear(), dp.getMonth(), dp.getDayOfMonth());
                    }
                });
        picker.setButton(DialogInterface.BUTTON_NEGATIVE, getActivity().getString(android.R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
    }
    return picker;
}

From source file:net.davidcesarino.android.common.ui.DatePickerDialogFragment.java

@TargetApi(11)
@Override//from ww w .  j  av  a 2s.c om
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Bundle b = getArguments();
    int y = b.getInt(YEAR);
    int m = b.getInt(MONTH);
    int d = b.getInt(DATE);

    // Jelly Bean introduced a bug in DatePickerDialog (and possibly 
    // TimePickerDialog as well), and one of the possible solutions is 
    // to postpone the creation of both the listener and the BUTTON_* .
    // 
    // Passing a null here won't harm because DatePickerDialog checks for a null
    // whenever it reads the listener that was passed here. >>> This seems to be 
    // true down to 1.5 / API 3, up to 4.1.1 / API 16. <<< No worries. For now.
    //
    // See my own question and answer, and details I included for the issue:
    //
    // http://stackoverflow.com/a/11493752/489607
    // http://code.google.com/p/android/issues/detail?id=34833
    //
    // Of course, suggestions welcome.

    final DatePickerDialog picker = new DatePickerDialog(getActivity(), getConstructorListener(), y, m, d);

    if (isAffectedVersion()) {
        picker.setButton(DialogInterface.BUTTON_POSITIVE, getActivity().getString(android.R.string.ok),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DatePicker dp = picker.getDatePicker();
                        mListener.onDateSet(dp, dp.getYear(), dp.getMonth(), dp.getDayOfMonth());
                    }
                });
        picker.setButton(DialogInterface.BUTTON_NEGATIVE, getActivity().getString(android.R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
    }
    return picker;
}

From source file:de.grobox.liberario.fragments.TimeDateFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_time_date, container);

    // Time//  ww w .  j a  v  a 2  s .c om
    timePicker = (TimePicker) v.findViewById(R.id.timePicker);
    timePicker.setOnTimeChangedListener(this);
    showTime(calendar);

    // Date
    dateView = (TextView) v.findViewById(R.id.dateView);
    dateView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            new DatePickerDialog(getContext(), TimeDateFragment.this, calendar.get(YEAR), calendar.get(MONTH),
                    calendar.get(DAY_OF_MONTH)).show();
        }
    });
    showDate(calendar);

    // Previous and Next Date
    ImageButton prevDateButton = (ImageButton) v.findViewById(R.id.prevDateButton);
    prevDateButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            calendar.add(DAY_OF_MONTH, -1);
            showDate(calendar);
        }
    });
    ImageButton nextDateButton = (ImageButton) v.findViewById(R.id.nextDateButton);
    nextDateButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            calendar.add(DAY_OF_MONTH, 1);
            showDate(calendar);
        }
    });

    // Buttons
    Button okButton = (Button) v.findViewById(R.id.okButton);
    okButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (listener != null) {
                boolean now = DateUtils.isNow(calendar);
                boolean today = DateUtils.isToday(calendar);
                listener.onTimeAndDateSet(calendar, now, today);
            }
            dismiss();
        }
    });
    Button cancelButton = (Button) v.findViewById(R.id.cancelButton);
    cancelButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            dismiss();
        }
    });

    return v;
}

From source file:com.example.android.rowanparkingpass.Activities.PassActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pass);
    i = 0;/*from w ww .  j ava2 s  .  co m*/

    Intent pastIntent = getIntent();
    currentMode = pastIntent.getStringExtra(MODE);

    db = new DatabaseHandlerPasses(getApplicationContext());

    Pass pass = (Pass) pastIntent.getSerializableExtra("Pass");
    if (pass == null) {
        driver = (Driver) pastIntent.getSerializableExtra("Driver");
        vehicle = (Vehicle) pastIntent.getSerializableExtra("Vehicle");
    } else {
        driver = pass.getDriver();
        vehicle = pass.getVehicle();
    }

    setDriverView();
    setVehicleView();

    dateFormatter = new SimpleDateFormat("MM/dd/yyyy");

    startDate = (EditText) findViewById(R.id.createstartdatefield);
    startDate.setInputType(InputType.TYPE_NULL);
    startDate.setOnClickListener(this);

    endDate = (EditText) findViewById(R.id.createenddatefield);
    endDate.setInputType(InputType.TYPE_NULL);
    endDate.setOnClickListener(this);

    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    calendar.set(year, month, day);

    startDate.setText(dateFormatter.format(calendar.getTime()));
    calendar.add(Calendar.DAY_OF_MONTH, 1);
    endDate.setText(dateFormatter.format(calendar.getTime()));

    startDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            startDate.setText(dateFormatter.format(newDate.getTime()));
            try {
                Date d = newDate.getTime();
                Date d2 = dateFormatter.parse(endDate.getText().toString());
                if (d.after(d2)) {
                    newDate.add(Calendar.DAY_OF_MONTH, 1);
                    endDate.setText(dateFormatter.format(newDate.getTime()));
                }
            } catch (ParseException pe) {
                pe.printStackTrace();
            }

        }
    }, year, month, day);

    endDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            endDate.setText(dateFormatter.format(newDate.getTime()));
            try {
                Date d = newDate.getTime();
                Date d2 = dateFormatter.parse(startDate.getText().toString());
                if (d.before(d2)) {
                    newDate.add(Calendar.DAY_OF_MONTH, -1);
                    startDate.setText(dateFormatter.format(newDate.getTime()));
                }
            } catch (ParseException pe) {
                pe.printStackTrace();
            }
        }
    }, year, month, day);

    createPass = (Button) findViewById(R.id.createPassButton);
    mainMenu = (Button) findViewById(R.id.goMainMenuButton);

    createPass.setOnClickListener(this);
    mainMenu.setOnClickListener(this);
}

From source file:com.magizdev.babyoneday.profilewizard.BirthDataFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(com.magizdev.babyoneday.R.layout.fragment_profilewizard_birthdata,
            container, false);/* w w  w .  j  av a2 s. com*/
    ((TextView) rootView.findViewById(android.R.id.title)).setText(mPage.getTitle());

    mHeightView = ((TextView) rootView.findViewById(com.magizdev.babyoneday.R.id.profile_height_edittext));
    mHeightView.setText(Float.toString(mPage.getData().getFloat(Profile.HEIGHT)));

    mWeightView = (TextView) rootView.findViewById(com.magizdev.babyoneday.R.id.profile_weight_edittext);
    mWeightView.setText(Float.toString(mPage.getData().getFloat(Profile.WEIGHT)));

    mBirthday = (TextView) rootView.findViewById(R.id.profile_birthday);
    mBirthdayBtn = (ImageButton) rootView.findViewById(com.magizdev.babyoneday.R.id.profile_birthday_set);
    int intBirthday = mPage.getData().getInt(Profile.BIRTHDAY);
    calendar = DayUtil.toCalendar(intBirthday);

    mBirthday.setText(birthdayString + ":" + DayUtil.formatCalendar(calendar));

    mBirthdayBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            DatePickerDialog dialog = new DatePickerDialog(getActivity(), BirthDataFragment.this,
                    calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH));
            dialog.show();
        }
    });
    birthdayString = getActivity().getResources().getString(com.magizdev.babyoneday.R.string.profile_birthday);

    return rootView;
}

From source file:com.flowzr.budget.holo.activity.RecurActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Setup ActionBar      
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    setContentView(R.layout.recur);/*from  w  w  w . ja  va2  s.  c o  m*/

    df = DateUtils.getLongDateFormat(this);

    stopsOnDate.add(Calendar.YEAR, 1);

    sInterval = (Spinner) findViewById(R.id.intervalSpinner);
    sPeriod = (Spinner) findViewById(R.id.recurSpinner);
    layoutInterval = (LinearLayout) findViewById(R.id.layoutInterval);
    layoutRecur = (LinearLayout) findViewById(R.id.recurInterval);

    bStartDate = (Button) findViewById(R.id.bStartDate);
    bStartDate.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            final Calendar c = startDate;
            DatePickerDialog d = new DatePickerDialog(RecurActivity.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);
                            DateUtils.startOfDay(c);
                            editStartDate(c.getTimeInMillis());
                        }
                    }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
            d.show();
        }
    });

    addSpinnerItems(sInterval,
            new RecurInterval[] { RecurInterval.NO_RECUR, RecurInterval.WEEKLY, RecurInterval.MONTHLY });
    addSpinnerItems(sPeriod, periods);

    LayoutInflater inflater = getLayoutInflater();
    //addLayouts(inflater, layoutInterval, intervals);
    addLayouts(inflater, layoutRecur, periods);

    Recur recur = RecurUtils.createDefaultRecur();
    Intent intent = getIntent();
    if (intent != null) {
        String extra = intent.getStringExtra(EXTRA_RECUR);
        if (extra != null) {
            recur = RecurUtils.createFromExtraString(extra);
        }
    }
    editRecur(recur);

    sInterval.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            RecurInterval interval = getRecurInterval(sInterval.getSelectedItem());
            selectInterval(interval);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }

    });

    sPeriod.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            RecurPeriod period = periods[position];
            selectPeriod(period);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }

    });

}

From source file:com.app.afridge.ui.fragments.DatePickerDialogFragment.java

@TargetApi(11)
@Override//from   w  w w .  ja v  a  2 s  . c o m
public Dialog onCreateDialog(Bundle savedInstanceState) {

    Bundle b = getArguments();
    int y = b.getInt(YEAR);
    int m = b.getInt(MONTH);
    int d = b.getInt(DATE);

    // Jelly Bean introduced a bug in DatePickerDialog (and possibly
    // TimePickerDialog as well), and one of the possible solutions is
    // to postpone the creation of both the listener and the BUTTON_* .
    //
    // Passing a null here won't harm because DatePickerDialog checks for a null
    // whenever it reads the listener that was passed here. >>> This seems to be
    // true down to 1.5 / API 3, up to 4.1.1 / API 16. <<< No worries. For now.
    //
    // See my own question and answer, and details I included for the issue:
    //
    // http://stackoverflow.com/a/11493752/489607
    // http://code.google.com/p/android/issues/detail?id=34833
    //
    // Of course, suggestions welcome.

    final DatePickerDialog picker = new DatePickerDialog(getActivity(), getConstructorListener(), y, m, d);

    if (hasJellyBeanAndAbove()) {
        picker.setButton(DialogInterface.BUTTON_POSITIVE, getActivity().getString(android.R.string.ok),
                new DialogInterface.OnClickListener() {

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

                        DatePicker dp = picker.getDatePicker();
                        mListener.onDateSet(dp, dp.getYear(), dp.getMonth(), dp.getDayOfMonth());
                    }
                });
        picker.setButton(DialogInterface.BUTTON_NEGATIVE, getActivity().getString(android.R.string.cancel),
                new DialogInterface.OnClickListener() {

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

                    }
                });
    }
    return picker;
}

From source file:com.propelforwardmedia.app.DatePickerDialogFragment.java

@TargetApi(11)
@Override/*from w w w  . j a v  a  2  s  .  c o m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
    int year = mCalendar.get(Calendar.YEAR);
    int month = mCalendar.get(Calendar.MONTH);
    int day = mCalendar.get(Calendar.DAY_OF_MONTH);

    // Jelly Bean introduced a bug in DatePickerDialog (and possibly 
    // TimePickerDialog as well), and one of the possible solutions is 
    // to postpone the creation of both the listener and the BUTTON_* .
    // 
    // Passing a null here won't harm because DatePickerDialog checks for a null
    // whenever it reads the listener that was passed here. >>> This seems to be 
    // true down to 1.5 / API 3, up to 4.1.1 / API 16. <<< No worries. For now.
    //
    // See my own question and answer, and details I included for the issue:
    //
    // http://stackoverflow.com/a/11493752/489607
    // http://code.google.com/p/android/issues/detail?id=34833
    //
    // Of course, suggestions welcome.

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

    if (hasJellyBeanAndAbove()) {
        picker.setButton(DialogInterface.BUTTON_POSITIVE, getActivity().getString(android.R.string.ok),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DatePicker dp = picker.getDatePicker();
                        mListener.onDateSet(dp, dp.getYear(), dp.getMonth(), dp.getDayOfMonth());
                    }
                });
        picker.setButton(DialogInterface.BUTTON_NEGATIVE, getActivity().getString(android.R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
    }
    return picker;
}