blob: 8b70370972ccfd1eee65f14257efea0727c0b195 [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
Svetoslav Ganov50f34d12010-12-03 16:05:40 -080019import com.android.internal.R;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
22import android.content.DialogInterface;
23import android.content.DialogInterface.OnClickListener;
24import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.view.LayoutInflater;
26import android.view.View;
27import android.widget.DatePicker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.widget.DatePicker.OnDateChangedListener;
29
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030/**
31 * A simple dialog containing an {@link android.widget.DatePicker}.
Scott Main41ec6532010-08-19 16:57:07 -070032 *
33 * <p>See the <a href="{@docRoot}resources/tutorials/views/hello-datepicker.html">Date Picker
34 * tutorial</a>.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 */
Christian Mehlmauere8db3a32010-06-25 20:01:03 +020036public class DatePickerDialog extends AlertDialog implements OnClickListener,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 OnDateChangedListener {
38
39 private static final String YEAR = "year";
40 private static final String MONTH = "month";
41 private static final String DAY = "day";
Christian Mehlmauere8db3a32010-06-25 20:01:03 +020042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 private final DatePicker mDatePicker;
44 private final OnDateSetListener mCallBack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
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 Ganovbf805622010-12-17 16:00:18 -080073 this(context, 0, callBack, year, monthOfYear, dayOfMonth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 }
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 Mehlmauere8db3a32010-06-25 20:01:03 +020093
94 setButton(BUTTON_POSITIVE, context.getText(R.string.date_time_set), this);
95 setButton(BUTTON_NEGATIVE, context.getText(R.string.cancel), (OnClickListener) null);
Svetoslav Ganov50f34d12010-12-03 16:05:40 -080096 setIcon(0);
97 setTitle(R.string.date_picker_dialog_title);
Christian Mehlmauere8db3a32010-06-25 20:01:03 +020098
99 LayoutInflater inflater =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 (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 Ganov50f34d12010-12-03 16:05:40 -0800104 mDatePicker.init(year, monthOfYear, dayOfMonth, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 }
Christian Mehlmauere8db3a32010-06-25 20:01:03 +0200106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 public void onClick(DialogInterface dialog, int which) {
108 if (mCallBack != null) {
109 mDatePicker.clearFocus();
Christian Mehlmauere8db3a32010-06-25 20:01:03 +0200110 mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
112 }
113 }
Christian Mehlmauere8db3a32010-06-25 20:01:03 +0200114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public void onDateChanged(DatePicker view, int year,
116 int month, int day) {
Svetoslav Ganov50f34d12010-12-03 16:05:40 -0800117 mDatePicker.init(year, month, day, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 }
Christian Mehlmauere8db3a32010-06-25 20:01:03 +0200119
Svetoslav Ganov28104e12010-12-19 16:03:07 -0800120 /**
Svetoslav Ganove9730bf2010-12-20 21:25:20 -0800121 * Gets the {@link DatePicker} contained in this dialog.
Svetoslav Ganov28104e12010-12-19 16:03:07 -0800122 *
Svetoslav Ganove9730bf2010-12-20 21:25:20 -0800123 * @return The calendar view.
Svetoslav Ganov28104e12010-12-19 16:03:07 -0800124 */
Svetoslav Ganove9730bf2010-12-20 21:25:20 -0800125 public DatePicker getDatePicker() {
126 return mDatePicker;
Svetoslav Ganov28104e12010-12-19 16:03:07 -0800127 }
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 Project9066cfe2009-03-03 19:31:44 -0800136 public void updateDate(int year, int monthOfYear, int dayOfMonth) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
138 }
139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 @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 Mehlmauere8db3a32010-06-25 20:01:03 +0200148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 @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 Project9066cfe2009-03-03 19:31:44 -0800156 }
157}