Showing posts with label TimePickerDialog. Show all posts
Showing posts with label TimePickerDialog. Show all posts

Deprecated: How to Display a Time 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 myTimeButton.

2. Add a private class level integer constant named TIME_PICKER_DIALOG and set 1.

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

return showTimePicker();

4. Add a New method named updateTimeButtonText 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. Instantiate a TimePickerDialog object with the TimePickerDialog initial values of:

hour of: myCalendar.get(Calendar.HOUR_OF_DAY), 
minute of: myCalendar.get(Calendar.MINUTE), 
AM/PM of: true

7. In the onTimeSet 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 myTimeButton to the myString variable.

10. Compile and run!

Resources:

How to Instantiate a TimePickerDialog object

public TimePickerDialog (Context context, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) was added in API level 1
Parameters:
context - Parent.
callBack - How parent is notified.
hourOfDay - The initial hour.
minute - The initial minute.
is24HourView - Whether this is a 24 hour view, or AM/PM.

is24HourView set to true

1. In the MainActivity.java file, Declare a TimePickerDialog object named myTimePickerDialog in a private method with a return value, with the method named showTimePicker and and return value type of TimePickerDialog.

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

import android.widget.TimePicker;

3. Add the below code to the showTimePicker method. This will set the TimePickerDialog and initialize the hour and minute to 3, 54, and true respectively. This will display the time of 3 hours, 54 minutes, in 24 hour time(without an AM/PM button). If it is set to false, the time will be in 12 hour format and the AM/PM button will display. Adding a toast will display the hour, minute, and AM/PM the user selected in the TimePickerDialog.  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.

timePicker = new TimePickerDialog(MyClassName.this, new TimePickerDialog.OnTimeSetListener()  {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// the onTimeSet method will get the hour and minute AFTER the user clicks the set button on the TimePickerDialog
//To test, add a toast, using a
                        //context of:  getBaseContext()
                        //and a message of:  ""Hour: " + hourOfDay + " Minute: " + minute
    }
} , 7, 54, true); // <- This will set the default time that will display on the TimePickerDialog when displayed

4. Compile and run!

is24HourView set to false

Resources:
http://developer.android.com/reference/android/app/TimePickerDialog.html#TimePickerDialog(android.content.Context, android.app.TimePickerDialog.OnTimeSetListener, int, int, boolean)

How to Declare a TimePickerDialog object

TimePickerDialag was added in API Level 1
A dialog that prompts the user for the time of day using a TimePicker.
This will declare a new TimePickerDialog object.
1. Add the import below to the import section of the MainActivity.java file.

import android.app.TimePickerDialog;

2. Add the below line to the onCreate method.

TimePickerDialog myTimePickerDialog;

3. Compile and run!

Next Recommended Article: How to Instantiate a TimePickerDialog object

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