Showing posts with label DatePickerDialog. Show all posts
Showing posts with label DatePickerDialog. Show all posts

Deprecated: How to Display a Date Picker Dialog from a Button

Note: Some of these steps use deprecated items.


1. Create a Click Handler for a Button, using a method named registerButtonListenersAndSetDefaultText and a private class level defined Button named myDateButton.

2. Add a private class level integer constant named DATE_PICKER_DIALOG and set 0.

3. Add a Deprecated: onCreateDialog method to your class. Add the showDialog  with a constant of DATE_PICKER_DIALOG to the onClick in the setOnClickListener in step 1. Add a case, named DATE_PICKER_DIALOG. In the case add the below line.

return showDatePicker();

4. Add a New method named updateDateButtonText to your class, and call it from the registerButtonListenersAndSetDefaultText method, after the setOnClickListener.

5. In the onCreate method, Get Current Date/Time and Save to Calendar object with a private class level declared Calendar object named myCalendar.

6. Set DatePickerDialog object with the DatePickerDialog initial values of myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH), and
myCalendar.get(Calendar.DAY_OF_MONTH)

7. In the onDateSet method,
    - set the Calendar.YEAR to the year variable
    - set the Calendar.MONTH to the monthOfYear variable
    - set the Calendar.DAY_OF_MONTH to the dayOfMonth variable
    - then call the updateDateButtonText method

8. In the updateDateButtonText method, add a Format Calendar Date using SimpleDateFormat with a format of "yyyy-MM-dd".

9. Then set the Text for the Button named myDateButton to the myString variable.

10. Compile and run!

Resources:

Set DatePickerDialog results to a Calendar object

public final void set (int year, int month, int day) was added in API level 1
Sets the year, month, and day of the month fields. Other fields are not changed; call clear() first if this is not desired. The month value is 0-based, so it may be clearer to use a constant like JANUARY.
0-January, 1-February, 2-March, 3-April, 4-May, 5-June, 6-July, 7-August, 8-September, 9-October, 10-November, 11-December


2. In theonDateSet method, add the line below to set the Calendar object date. If the user selects the date in the DatePickerDialog of  December 25, 2014, the Calendar object would be set to that date.

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
// this method will get the year, month and day the user chose on the datepickerdialog

    myCalendar.set(year, monthOfYear, dayOfMonth)
    // To test, add a Toast with a
        //context of getBaseContext()
       //and a message of: "Month: " + myCalendar.get(MONTH) + "Day: " + myCalendar.get(Calendar.DAY_OF_MONTH) + "Year: " + myCalendar.get(Calendar.YEAR)
}

How to Instantiate a DatePickerDialog object

public DatePickerDialog (Context context, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) was added in API level 1
Parameters:
context - The context the dialog is to run in.
callBack - How the parent is notified that the date is set.
year - The initial year of the dialog.
monthOfYear - The initial month of the dialog.
dayOfMonth - The initial day of the dialog.

1. In the MainActivity.java file, Declare a DatePickerDialog object named myDatePickerDialog  in a private method with a return value, with the method named showDatePicker and and return value type of DatePickerDialog.

2. Also, add the below import to the imports section.

import android.widget.DatePicker;

3. Add the below code to the showDatePicker method. This will set the DatePickerDialog and initialize the year, month and day to 1999, 9 and 24 respectively. Adding a toast will display the year, month and day the user selected in the DatePickerDialog.  This below would be in a class named MyClassName. You would change this to the class it is currently located within. The full class name is included because it’s inside a nested statement, therefore fully qualified names are required.

datePicker = new DatePickerDialog(MyClassName.this, new DatePickerDialog.OnDateSetListener()  {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// the onDateSet method will get the year, month and day AFTER the user clicks the set button on the DatePickerDialog
//To test, add a toast, using a
                        //context of:  getBaseContext()
                        //and a message of:  ""Year: " + year + " Month: " + monthOfYear + " Day: " + dayOfMonth
    }
} , 1999, 9, 24); // <- This will set the default date that will display on the DatePickerDialog when displayed

4. Compile and run!

Next Recommended Article: Set DatePickerDialog results to a Calendar object

Resources:
http://developer.android.com/reference/android/app/DatePickerDialog.html#DatePickerDialog(android.content.Context, android.app.DatePickerDialog.OnDateSetListener, int, int, int)

How to Declare a new DatePickerDialog object

DatePickerDialag was added in API Level 1
A simple dialog containing an DatePicker.
This will declare a new DatePickerDialog object.

1. Add the import below to the import section of the MainActivity.java file.

import android.app.DatePickerDialog;

2. Add the below line to the onCreate method.

DatePickerDialog myDatePickerDialog;

3. Compile and run!

Next Recommended Article: How to Instantiate a DatePickerDialog object

Resources:
http://developer.android.com/reference/android/app/DatePickerDialog.html