blob: 83dc506371cf5738a4cc43fad75c696f83dccab9 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.app;
18
Alan Viverettead50da52015-12-17 11:41:05 -050019import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.annotation.StyleRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
23import android.content.DialogInterface;
24import android.content.DialogInterface.OnClickListener;
25import android.os.Bundle;
Fabrice Di Megliobd9152f2013-10-01 11:21:31 -070026import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.view.LayoutInflater;
28import android.view.View;
Alan Viverette518ff0d2014-08-15 14:20:35 -070029import android.widget.Button;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.widget.DatePicker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.widget.DatePicker.OnDateChangedListener;
Alan Viverette518ff0d2014-08-15 14:20:35 -070032import android.widget.DatePicker.ValidationCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Svetoslav Ganov42c5cb32012-05-02 01:02:20 -070034import com.android.internal.R;
35
36import java.util.Calendar;
37
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038/**
39 * A simple dialog containing an {@link android.widget.DatePicker}.
Alan Viverettead50da52015-12-17 11:41:05 -050040 * <p>
41 * See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a>
42 * guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 */
Christian Mehlmauere8db3a32010-06-25 20:01:03 +020044public class DatePickerDialog extends AlertDialog implements OnClickListener,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 OnDateChangedListener {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 private static final String YEAR = "year";
47 private static final String MONTH = "month";
48 private static final String DAY = "day";
Christian Mehlmauere8db3a32010-06-25 20:01:03 +020049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 private final DatePicker mDatePicker;
Svetoslav Ganov42c5cb32012-05-02 01:02:20 -070051
Alan Viverettead50da52015-12-17 11:41:05 -050052 private OnDateSetListener mDateSetListener;
53
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 /**
Alan Viverettead50da52015-12-17 11:41:05 -050055 * 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 Project9066cfe2009-03-03 19:31:44 -080059 */
Alan Viverette8ae4ff72016-01-13 11:13:50 -050060 public DatePickerDialog(@NonNull Context context) {
Alan Viverettead50da52015-12-17 11:41:05 -050061 this(context, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 }
63
64 /**
Alan Viverettead50da52015-12-17 11:41:05 -050065 * 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 Project9066cfe2009-03-03 19:31:44 -080071 */
Alan Viverette8ae4ff72016-01-13 11:13:50 -050072 public DatePickerDialog(@NonNull Context context, @StyleRes int themeResId) {
Alan Viverettead50da52015-12-17 11:41:05 -050073 super(context, resolveDialogTheme(context, themeResId));
Svetoslav Ganov42c5cb32012-05-02 01:02:20 -070074
Alan Viverette518ff0d2014-08-15 14:20:35 -070075 final Context themeContext = getContext();
76 final LayoutInflater inflater = LayoutInflater.from(themeContext);
Alan Viverette60727e02014-07-28 16:56:32 -070077 final View view = inflater.inflate(R.layout.date_picker_dialog, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 setView(view);
Alan Viverettead50da52015-12-17 11:41:05 -050079
Alan Viverette518ff0d2014-08-15 14:20:35 -070080 setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this);
81 setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this);
Craig Stout4c0cb8a2014-04-04 13:03:10 -070082 setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);
Fabrice Di Megliobd9152f2013-10-01 11:21:31 -070083
Alan Viverette8ae4ff72016-01-13 11:13:50 -050084 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 Viverette60727e02014-07-28 16:56:32 -070088 mDatePicker = (DatePicker) view.findViewById(R.id.datePicker);
Svetoslav Ganov50f34d12010-12-03 16:05:40 -080089 mDatePicker.init(year, monthOfYear, dayOfMonth, this);
Alan Viverette518ff0d2014-08-15 14:20:35 -070090 mDatePicker.setValidationCallback(mValidationCallback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 }
Christian Mehlmauere8db3a32010-06-25 20:01:03 +020092
Alan Viverettead50da52015-12-17 11:41:05 -050093 /**
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 Viverette8ae4ff72016-01-13 11:13:50 -0500105 public DatePickerDialog(@NonNull Context context, @Nullable OnDateSetListener listener,
Alan Viverettead50da52015-12-17 11:41:05 -0500106 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 Viverettead50da52015-12-17 11:41:05 -0500128 mDatePicker.updateDate(year, month, dayOfMonth);
Alan Viverette4d1ad992016-03-08 18:48:47 -0500129 mDateSetListener = listener;
Alan Viverettead50da52015-12-17 11:41:05 -0500130 }
131
Alan Viverette8ae4ff72016-01-13 11:13:50 -0500132 static @StyleRes int resolveDialogTheme(@NonNull Context context, @StyleRes int themeResId) {
Alan Viverettead50da52015-12-17 11:41:05 -0500133 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 Megliobd9152f2013-10-01 11:21:31 -0700142 @Override
Alan Viverette8ae4ff72016-01-13 11:13:50 -0500143 public void onDateChanged(@NonNull DatePicker view, int year, int month, int dayOfMonth) {
Alan Viverettead50da52015-12-17 11:41:05 -0500144 mDatePicker.init(year, month, dayOfMonth, this);
Alan Viverettead50da52015-12-17 11:41:05 -0500145 }
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 Project9066cfe2009-03-03 19:31:44 -0800154 }
Christian Mehlmauere8db3a32010-06-25 20:01:03 +0200155
Alan Viverette518ff0d2014-08-15 14:20:35 -0700156 @Override
Alan Viverette8ae4ff72016-01-13 11:13:50 -0500157 public void onClick(@NonNull DialogInterface dialog, int which) {
Alan Viverette518ff0d2014-08-15 14:20:35 -0700158 switch (which) {
159 case BUTTON_POSITIVE:
160 if (mDateSetListener != null) {
Alan Viveretteb2b98a00e2015-01-13 17:34:17 -0800161 // Clearing focus forces the dialog to commit any pending
162 // changes, e.g. typed text in a NumberPicker.
163 mDatePicker.clearFocus();
Alan Viverette518ff0d2014-08-15 14:20:35 -0700164 mDateSetListener.onDateSet(mDatePicker, mDatePicker.getYear(),
165 mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
166 }
167 break;
Alan Viverette16a24bc2014-08-28 11:19:59 -0700168 case BUTTON_NEGATIVE:
169 cancel();
170 break;
Fabrice Di Megliobd9152f2013-10-01 11:21:31 -0700171 }
172 }
173
Svetoslav Ganov28104e12010-12-19 16:03:07 -0800174 /**
Alan Viverettead50da52015-12-17 11:41:05 -0500175 * Returns the {@link DatePicker} contained in this dialog.
Svetoslav Ganov28104e12010-12-19 16:03:07 -0800176 *
Alan Viverettead50da52015-12-17 11:41:05 -0500177 * @return the date picker
Svetoslav Ganov28104e12010-12-19 16:03:07 -0800178 */
Alan Viverettead50da52015-12-17 11:41:05 -0500179 @NonNull
Svetoslav Ganove9730bf2010-12-20 21:25:20 -0800180 public DatePicker getDatePicker() {
181 return mDatePicker;
Svetoslav Ganov28104e12010-12-19 16:03:07 -0800182 }
183
184 /**
185 * Sets the current date.
186 *
Alan Viverettead50da52015-12-17 11:41:05 -0500187 * @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 Ganov28104e12010-12-19 16:03:07 -0800191 */
Alan Viverettead50da52015-12-17 11:41:05 -0500192 public void updateDate(int year, int month, int dayOfMonth) {
193 mDatePicker.updateDate(year, month, dayOfMonth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 }
195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 @Override
197 public Bundle onSaveInstanceState() {
Alan Viverette518ff0d2014-08-15 14:20:35 -0700198 final Bundle state = super.onSaveInstanceState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 state.putInt(YEAR, mDatePicker.getYear());
200 state.putInt(MONTH, mDatePicker.getMonth());
201 state.putInt(DAY, mDatePicker.getDayOfMonth());
202 return state;
203 }
Christian Mehlmauere8db3a32010-06-25 20:01:03 +0200204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 @Override
206 public void onRestoreInstanceState(Bundle savedInstanceState) {
207 super.onRestoreInstanceState(savedInstanceState);
Alan Viverette518ff0d2014-08-15 14:20:35 -0700208 final int year = savedInstanceState.getInt(YEAR);
209 final int month = savedInstanceState.getInt(MONTH);
210 final int day = savedInstanceState.getInt(DAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 mDatePicker.init(year, month, day, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 }
Alan Viverette518ff0d2014-08-15 14:20:35 -0700213
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 Viverettead50da52015-12-17 11:41:05 -0500223
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 Project9066cfe2009-03-03 19:31:44 -0800238}