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