The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 19 | import com.android.internal.R; |
| 20 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.DialogInterface; |
| 23 | import android.content.DialogInterface.OnClickListener; |
| 24 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.view.LayoutInflater; |
| 26 | import android.view.View; |
| 27 | import android.widget.DatePicker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.widget.DatePicker.OnDateChangedListener; |
| 29 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | /** |
| 31 | * A simple dialog containing an {@link android.widget.DatePicker}. |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 32 | * |
| 33 | * <p>See the <a href="{@docRoot}resources/tutorials/views/hello-datepicker.html">Date Picker |
| 34 | * tutorial</a>.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | */ |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 36 | public class DatePickerDialog extends AlertDialog implements OnClickListener, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | OnDateChangedListener { |
| 38 | |
| 39 | private static final String YEAR = "year"; |
| 40 | private static final String MONTH = "month"; |
| 41 | private static final String DAY = "day"; |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 42 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | private final DatePicker mDatePicker; |
| 44 | private final OnDateSetListener mCallBack; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * The callback used to indicate the user is done filling in the date. |
| 48 | */ |
| 49 | public interface OnDateSetListener { |
| 50 | |
| 51 | /** |
| 52 | * @param view The view associated with this listener. |
| 53 | * @param year The year that was set. |
| 54 | * @param monthOfYear The month that was set (0-11) for compatibility |
| 55 | * with {@link java.util.Calendar}. |
| 56 | * @param dayOfMonth The day of the month that was set. |
| 57 | */ |
| 58 | void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @param context The context the dialog is to run in. |
| 63 | * @param callBack How the parent is notified that the date is set. |
| 64 | * @param year The initial year of the dialog. |
| 65 | * @param monthOfYear The initial month of the dialog. |
| 66 | * @param dayOfMonth The initial day of the dialog. |
| 67 | */ |
| 68 | public DatePickerDialog(Context context, |
| 69 | OnDateSetListener callBack, |
| 70 | int year, |
| 71 | int monthOfYear, |
| 72 | int dayOfMonth) { |
Svetoslav Ganov | bf80562 | 2010-12-17 16:00:18 -0800 | [diff] [blame] | 73 | this(context, 0, callBack, year, monthOfYear, dayOfMonth); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param context The context the dialog is to run in. |
| 78 | * @param theme the theme to apply to this dialog |
| 79 | * @param callBack How the parent is notified that the date is set. |
| 80 | * @param year The initial year of the dialog. |
| 81 | * @param monthOfYear The initial month of the dialog. |
| 82 | * @param dayOfMonth The initial day of the dialog. |
| 83 | */ |
| 84 | public DatePickerDialog(Context context, |
| 85 | int theme, |
| 86 | OnDateSetListener callBack, |
| 87 | int year, |
| 88 | int monthOfYear, |
| 89 | int dayOfMonth) { |
| 90 | super(context, theme); |
| 91 | |
| 92 | mCallBack = callBack; |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 93 | |
| 94 | setButton(BUTTON_POSITIVE, context.getText(R.string.date_time_set), this); |
| 95 | setButton(BUTTON_NEGATIVE, context.getText(R.string.cancel), (OnClickListener) null); |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 96 | setIcon(0); |
| 97 | setTitle(R.string.date_picker_dialog_title); |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 98 | |
| 99 | LayoutInflater inflater = |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 101 | View view = inflater.inflate(R.layout.date_picker_dialog, null); |
| 102 | setView(view); |
| 103 | mDatePicker = (DatePicker) view.findViewById(R.id.datePicker); |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 104 | mDatePicker.init(year, monthOfYear, dayOfMonth, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | public void onClick(DialogInterface dialog, int which) { |
| 108 | if (mCallBack != null) { |
| 109 | mDatePicker.clearFocus(); |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 110 | mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | mDatePicker.getMonth(), mDatePicker.getDayOfMonth()); |
| 112 | } |
| 113 | } |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | public void onDateChanged(DatePicker view, int year, |
| 116 | int month, int day) { |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 117 | mDatePicker.init(year, month, day, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | } |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 119 | |
Svetoslav Ganov | 28104e1 | 2010-12-19 16:03:07 -0800 | [diff] [blame] | 120 | /** |
Svetoslav Ganov | e9730bf | 2010-12-20 21:25:20 -0800 | [diff] [blame] | 121 | * Gets the {@link DatePicker} contained in this dialog. |
Svetoslav Ganov | 28104e1 | 2010-12-19 16:03:07 -0800 | [diff] [blame] | 122 | * |
Svetoslav Ganov | e9730bf | 2010-12-20 21:25:20 -0800 | [diff] [blame] | 123 | * @return The calendar view. |
Svetoslav Ganov | 28104e1 | 2010-12-19 16:03:07 -0800 | [diff] [blame] | 124 | */ |
Svetoslav Ganov | e9730bf | 2010-12-20 21:25:20 -0800 | [diff] [blame] | 125 | public DatePicker getDatePicker() { |
| 126 | return mDatePicker; |
Svetoslav Ganov | 28104e1 | 2010-12-19 16:03:07 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Sets the current date. |
| 131 | * |
| 132 | * @param year The date year. |
| 133 | * @param monthOfYear The date month. |
| 134 | * @param dayOfMonth The date day of month. |
| 135 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | public void updateDate(int year, int monthOfYear, int dayOfMonth) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | mDatePicker.updateDate(year, monthOfYear, dayOfMonth); |
| 138 | } |
| 139 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | @Override |
| 141 | public Bundle onSaveInstanceState() { |
| 142 | Bundle state = super.onSaveInstanceState(); |
| 143 | state.putInt(YEAR, mDatePicker.getYear()); |
| 144 | state.putInt(MONTH, mDatePicker.getMonth()); |
| 145 | state.putInt(DAY, mDatePicker.getDayOfMonth()); |
| 146 | return state; |
| 147 | } |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 148 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | @Override |
| 150 | public void onRestoreInstanceState(Bundle savedInstanceState) { |
| 151 | super.onRestoreInstanceState(savedInstanceState); |
| 152 | int year = savedInstanceState.getInt(YEAR); |
| 153 | int month = savedInstanceState.getInt(MONTH); |
| 154 | int day = savedInstanceState.getInt(DAY); |
| 155 | mDatePicker.init(year, month, day, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | } |
| 157 | } |