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 | |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.annotation.StyleRes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.DialogInterface; |
| 24 | import android.content.DialogInterface.OnClickListener; |
| 25 | import android.os.Bundle; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 26 | import android.util.TypedValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.view.LayoutInflater; |
| 28 | import android.view.View; |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 29 | import android.widget.Button; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.widget.DatePicker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.widget.DatePicker.OnDateChangedListener; |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 32 | import android.widget.DatePicker.ValidationCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
Svetoslav Ganov | 42c5cb3 | 2012-05-02 01:02:20 -0700 | [diff] [blame] | 34 | import com.android.internal.R; |
| 35 | |
| 36 | import java.util.Calendar; |
| 37 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | /** |
| 39 | * A simple dialog containing an {@link android.widget.DatePicker}. |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 40 | * <p> |
| 41 | * See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a> |
| 42 | * guide. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | */ |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 44 | public class DatePickerDialog extends AlertDialog implements OnClickListener, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | OnDateChangedListener { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | private static final String YEAR = "year"; |
| 47 | private static final String MONTH = "month"; |
| 48 | private static final String DAY = "day"; |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 49 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | private final DatePicker mDatePicker; |
Svetoslav Ganov | 42c5cb3 | 2012-05-02 01:02:20 -0700 | [diff] [blame] | 51 | |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 52 | private OnDateSetListener mDateSetListener; |
| 53 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | /** |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 55 | * Creates a new date picker dialog for the current date using the parent |
| 56 | * context's default date picker dialog theme. |
| 57 | * |
| 58 | * @param context the parent context |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | */ |
Alan Viverette | 8ae4ff7 | 2016-01-13 11:13:50 -0500 | [diff] [blame] | 60 | public DatePickerDialog(@NonNull Context context) { |
Alan Viverette | a9a75f5 | 2016-04-01 13:22:10 -0400 | [diff] [blame] | 61 | this(context, 0, null, Calendar.getInstance(), -1, -1, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 65 | * Creates a new date picker dialog for the current date. |
| 66 | * |
| 67 | * @param context the parent context |
| 68 | * @param themeResId the resource ID of the theme against which to inflate |
| 69 | * this dialog, or {@code 0} to use the parent |
| 70 | * {@code context}'s default alert dialog theme |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | */ |
Alan Viverette | 8ae4ff7 | 2016-01-13 11:13:50 -0500 | [diff] [blame] | 72 | public DatePickerDialog(@NonNull Context context, @StyleRes int themeResId) { |
Alan Viverette | a9a75f5 | 2016-04-01 13:22:10 -0400 | [diff] [blame] | 73 | this(context, themeResId, null, Calendar.getInstance(), -1, -1, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | } |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 75 | |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 76 | /** |
| 77 | * Creates a new date picker dialog for the specified date using the parent |
| 78 | * context's default date picker dialog theme. |
| 79 | * |
| 80 | * @param context the parent context |
| 81 | * @param listener the listener to call when the user sets the date |
| 82 | * @param year the initially selected year |
| 83 | * @param month the initially selected month (0-11 for compatibility with |
| 84 | * {@link Calendar#MONTH}) |
| 85 | * @param dayOfMonth the initially selected day of month (1-31, depending |
| 86 | * on month) |
| 87 | */ |
Alan Viverette | 8ae4ff7 | 2016-01-13 11:13:50 -0500 | [diff] [blame] | 88 | public DatePickerDialog(@NonNull Context context, @Nullable OnDateSetListener listener, |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 89 | int year, int month, int dayOfMonth) { |
Alan Viverette | a9a75f5 | 2016-04-01 13:22:10 -0400 | [diff] [blame] | 90 | this(context, 0, listener, null, year, month, dayOfMonth); |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Creates a new date picker dialog for the specified date. |
| 95 | * |
| 96 | * @param context the parent context |
| 97 | * @param themeResId the resource ID of the theme against which to inflate |
| 98 | * this dialog, or {@code 0} to use the parent |
| 99 | * {@code context}'s default alert dialog theme |
| 100 | * @param listener the listener to call when the user sets the date |
| 101 | * @param year the initially selected year |
Alan Viverette | a9a75f5 | 2016-04-01 13:22:10 -0400 | [diff] [blame] | 102 | * @param monthOfYear the initially selected month of the year (0-11 for |
| 103 | * compatibility with {@link Calendar#MONTH}) |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 104 | * @param dayOfMonth the initially selected day of month (1-31, depending |
| 105 | * on month) |
| 106 | */ |
| 107 | public DatePickerDialog(@NonNull Context context, @StyleRes int themeResId, |
Alan Viverette | a9a75f5 | 2016-04-01 13:22:10 -0400 | [diff] [blame] | 108 | @Nullable OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) { |
| 109 | this(context, themeResId, listener, null, year, monthOfYear, dayOfMonth); |
| 110 | } |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 111 | |
Alan Viverette | a9a75f5 | 2016-04-01 13:22:10 -0400 | [diff] [blame] | 112 | private DatePickerDialog(@NonNull Context context, @StyleRes int themeResId, |
| 113 | @Nullable OnDateSetListener listener, @Nullable Calendar calendar, int year, |
| 114 | int monthOfYear, int dayOfMonth) { |
| 115 | super(context, resolveDialogTheme(context, themeResId)); |
| 116 | |
| 117 | final Context themeContext = getContext(); |
| 118 | final LayoutInflater inflater = LayoutInflater.from(themeContext); |
| 119 | final View view = inflater.inflate(R.layout.date_picker_dialog, null); |
| 120 | setView(view); |
| 121 | |
| 122 | setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this); |
| 123 | setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this); |
| 124 | setButtonPanelLayoutHint(LAYOUT_HINT_SIDE); |
| 125 | |
| 126 | if (calendar != null) { |
| 127 | year = calendar.get(Calendar.YEAR); |
| 128 | monthOfYear = calendar.get(Calendar.MONTH); |
| 129 | dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); |
| 130 | } |
| 131 | |
| 132 | mDatePicker = (DatePicker) view.findViewById(R.id.datePicker); |
| 133 | mDatePicker.init(year, monthOfYear, dayOfMonth, this); |
| 134 | mDatePicker.setValidationCallback(mValidationCallback); |
| 135 | |
Alan Viverette | 4d1ad99 | 2016-03-08 18:48:47 -0500 | [diff] [blame] | 136 | mDateSetListener = listener; |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Alan Viverette | 8ae4ff7 | 2016-01-13 11:13:50 -0500 | [diff] [blame] | 139 | static @StyleRes int resolveDialogTheme(@NonNull Context context, @StyleRes int themeResId) { |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 140 | if (themeResId == 0) { |
| 141 | final TypedValue outValue = new TypedValue(); |
| 142 | context.getTheme().resolveAttribute(R.attr.datePickerDialogTheme, outValue, true); |
| 143 | return outValue.resourceId; |
| 144 | } else { |
| 145 | return themeResId; |
| 146 | } |
| 147 | } |
| 148 | |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 149 | @Override |
Alan Viverette | 8ae4ff7 | 2016-01-13 11:13:50 -0500 | [diff] [blame] | 150 | public void onDateChanged(@NonNull DatePicker view, int year, int month, int dayOfMonth) { |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 151 | mDatePicker.init(year, month, dayOfMonth, this); |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Sets the listener to call when the user sets the date. |
| 156 | * |
| 157 | * @param listener the listener to call when the user sets the date |
| 158 | */ |
| 159 | public void setOnDateSetListener(@Nullable OnDateSetListener listener) { |
| 160 | mDateSetListener = listener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | } |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 162 | |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 163 | @Override |
Alan Viverette | 8ae4ff7 | 2016-01-13 11:13:50 -0500 | [diff] [blame] | 164 | public void onClick(@NonNull DialogInterface dialog, int which) { |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 165 | switch (which) { |
| 166 | case BUTTON_POSITIVE: |
| 167 | if (mDateSetListener != null) { |
Alan Viverette | b2b98a00e | 2015-01-13 17:34:17 -0800 | [diff] [blame] | 168 | // Clearing focus forces the dialog to commit any pending |
| 169 | // changes, e.g. typed text in a NumberPicker. |
| 170 | mDatePicker.clearFocus(); |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 171 | mDateSetListener.onDateSet(mDatePicker, mDatePicker.getYear(), |
| 172 | mDatePicker.getMonth(), mDatePicker.getDayOfMonth()); |
| 173 | } |
| 174 | break; |
Alan Viverette | 16a24bc | 2014-08-28 11:19:59 -0700 | [diff] [blame] | 175 | case BUTTON_NEGATIVE: |
| 176 | cancel(); |
| 177 | break; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Svetoslav Ganov | 28104e1 | 2010-12-19 16:03:07 -0800 | [diff] [blame] | 181 | /** |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 182 | * Returns the {@link DatePicker} contained in this dialog. |
Svetoslav Ganov | 28104e1 | 2010-12-19 16:03:07 -0800 | [diff] [blame] | 183 | * |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 184 | * @return the date picker |
Svetoslav Ganov | 28104e1 | 2010-12-19 16:03:07 -0800 | [diff] [blame] | 185 | */ |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 186 | @NonNull |
Svetoslav Ganov | e9730bf | 2010-12-20 21:25:20 -0800 | [diff] [blame] | 187 | public DatePicker getDatePicker() { |
| 188 | return mDatePicker; |
Svetoslav Ganov | 28104e1 | 2010-12-19 16:03:07 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Sets the current date. |
| 193 | * |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 194 | * @param year the year |
| 195 | * @param month the month (0-11 for compatibility with |
| 196 | * {@link Calendar#MONTH}) |
| 197 | * @param dayOfMonth the day of month (1-31, depending on month) |
Svetoslav Ganov | 28104e1 | 2010-12-19 16:03:07 -0800 | [diff] [blame] | 198 | */ |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 199 | public void updateDate(int year, int month, int dayOfMonth) { |
| 200 | mDatePicker.updateDate(year, month, dayOfMonth); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | } |
| 202 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | @Override |
| 204 | public Bundle onSaveInstanceState() { |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 205 | final Bundle state = super.onSaveInstanceState(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | state.putInt(YEAR, mDatePicker.getYear()); |
| 207 | state.putInt(MONTH, mDatePicker.getMonth()); |
| 208 | state.putInt(DAY, mDatePicker.getDayOfMonth()); |
| 209 | return state; |
| 210 | } |
Christian Mehlmauer | e8db3a3 | 2010-06-25 20:01:03 +0200 | [diff] [blame] | 211 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | @Override |
| 213 | public void onRestoreInstanceState(Bundle savedInstanceState) { |
| 214 | super.onRestoreInstanceState(savedInstanceState); |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 215 | final int year = savedInstanceState.getInt(YEAR); |
| 216 | final int month = savedInstanceState.getInt(MONTH); |
| 217 | final int day = savedInstanceState.getInt(DAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | mDatePicker.init(year, month, day, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | } |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 220 | |
| 221 | private final ValidationCallback mValidationCallback = new ValidationCallback() { |
| 222 | @Override |
| 223 | public void onValidationChanged(boolean valid) { |
| 224 | final Button positive = getButton(BUTTON_POSITIVE); |
| 225 | if (positive != null) { |
| 226 | positive.setEnabled(valid); |
| 227 | } |
| 228 | } |
| 229 | }; |
Alan Viverette | ad50da5 | 2015-12-17 11:41:05 -0500 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * The listener used to indicate the user has finished selecting a date. |
| 233 | */ |
| 234 | public interface OnDateSetListener { |
| 235 | /** |
| 236 | * @param view the picker associated with the dialog |
| 237 | * @param year the selected year |
| 238 | * @param month the selected month (0-11 for compatibility with |
| 239 | * {@link Calendar#MONTH}) |
| 240 | * @param dayOfMonth th selected day of the month (1-31, depending on |
| 241 | * month) |
| 242 | */ |
| 243 | void onDateSet(DatePicker view, int year, int month, int dayOfMonth); |
| 244 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | } |