DatePickerDialog.java revision 5bd644caf73e76750feef1a82b8817d32f5367fc
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 17package android.app; 18 19import android.content.Context; 20import android.content.DialogInterface; 21import android.content.DialogInterface.OnClickListener; 22import android.os.Bundle; 23import android.text.TextUtils.TruncateAt; 24import android.text.format.DateFormat; 25import android.view.LayoutInflater; 26import android.view.View; 27import android.widget.DatePicker; 28import android.widget.TextView; 29import android.widget.DatePicker.OnDateChangedListener; 30 31import com.android.internal.R; 32 33import java.text.DateFormatSymbols; 34import java.util.Calendar; 35 36/** 37 * A simple dialog containing an {@link android.widget.DatePicker}. 38 */ 39public class DatePickerDialog extends AlertDialog implements OnClickListener, 40 OnDateChangedListener { 41 42 private static final String YEAR = "year"; 43 private static final String MONTH = "month"; 44 private static final String DAY = "day"; 45 46 private final DatePicker mDatePicker; 47 private final OnDateSetListener mCallBack; 48 private final Calendar mCalendar; 49 private final java.text.DateFormat mTitleDateFormat; 50 private final String[] mWeekDays; 51 52 private int mInitialYear; 53 private int mInitialMonth; 54 private int mInitialDay; 55 56 /** 57 * The callback used to indicate the user is done filling in the date. 58 */ 59 public interface OnDateSetListener { 60 61 /** 62 * @param view The view associated with this listener. 63 * @param year The year that was set. 64 * @param monthOfYear The month that was set (0-11) for compatibility 65 * with {@link java.util.Calendar}. 66 * @param dayOfMonth The day of the month that was set. 67 */ 68 void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth); 69 } 70 71 /** 72 * @param context The context the dialog is to run in. 73 * @param callBack How the parent is notified that the date is set. 74 * @param year The initial year of the dialog. 75 * @param monthOfYear The initial month of the dialog. 76 * @param dayOfMonth The initial day of the dialog. 77 */ 78 public DatePickerDialog(Context context, 79 OnDateSetListener callBack, 80 int year, 81 int monthOfYear, 82 int dayOfMonth) { 83 this(context, com.android.internal.R.style.Theme_Dialog_Alert, 84 callBack, year, monthOfYear, dayOfMonth); 85 } 86 87 /** 88 * @param context The context the dialog is to run in. 89 * @param theme the theme to apply to this dialog 90 * @param callBack How the parent is notified that the date is set. 91 * @param year The initial year of the dialog. 92 * @param monthOfYear The initial month of the dialog. 93 * @param dayOfMonth The initial day of the dialog. 94 */ 95 public DatePickerDialog(Context context, 96 int theme, 97 OnDateSetListener callBack, 98 int year, 99 int monthOfYear, 100 int dayOfMonth) { 101 super(context, theme); 102 103 mCallBack = callBack; 104 mInitialYear = year; 105 mInitialMonth = monthOfYear; 106 mInitialDay = dayOfMonth; 107 DateFormatSymbols symbols = new DateFormatSymbols(); 108 mWeekDays = symbols.getShortWeekdays(); 109 110 mTitleDateFormat = java.text.DateFormat. 111 getDateInstance(java.text.DateFormat.FULL); 112 mCalendar = Calendar.getInstance(); 113 updateTitle(mInitialYear, mInitialMonth, mInitialDay); 114 115 setButton(context.getText(R.string.date_time_set), this); 116 setButton2(context.getText(R.string.cancel), (OnClickListener) null); 117 setIcon(R.drawable.ic_dialog_time); 118 119 LayoutInflater inflater = 120 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 121 View view = inflater.inflate(R.layout.date_picker_dialog, null); 122 setView(view); 123 mDatePicker = (DatePicker) view.findViewById(R.id.datePicker); 124 mDatePicker.init(mInitialYear, mInitialMonth, mInitialDay, this); 125 } 126 127 @Override 128 public void show() { 129 super.show(); 130 131 /* Sometimes the full month is displayed causing the title 132 * to be very long, in those cases ensure it doesn't wrap to 133 * 2 lines (as that looks jumpy) and ensure we ellipsize the end. 134 */ 135 TextView title = (TextView) findViewById(R.id.alertTitle); 136 title.setSingleLine(); 137 title.setEllipsize(TruncateAt.END); 138 } 139 140 public void onClick(DialogInterface dialog, int which) { 141 if (mCallBack != null) { 142 mDatePicker.clearFocus(); 143 mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(), 144 mDatePicker.getMonth(), mDatePicker.getDayOfMonth()); 145 } 146 } 147 148 public void onDateChanged(DatePicker view, int year, 149 int month, int day) { 150 updateTitle(year, month, day); 151 } 152 153 public void updateDate(int year, int monthOfYear, int dayOfMonth) { 154 mInitialYear = year; 155 mInitialMonth = monthOfYear; 156 mInitialDay = dayOfMonth; 157 mDatePicker.updateDate(year, monthOfYear, dayOfMonth); 158 } 159 160 private void updateTitle(int year, int month, int day) { 161 mCalendar.set(Calendar.YEAR, year); 162 mCalendar.set(Calendar.MONTH, month); 163 mCalendar.set(Calendar.DAY_OF_MONTH, day); 164 setTitle(mTitleDateFormat.format(mCalendar.getTime())); 165 } 166 167 @Override 168 public Bundle onSaveInstanceState() { 169 Bundle state = super.onSaveInstanceState(); 170 state.putInt(YEAR, mDatePicker.getYear()); 171 state.putInt(MONTH, mDatePicker.getMonth()); 172 state.putInt(DAY, mDatePicker.getDayOfMonth()); 173 return state; 174 } 175 176 @Override 177 public void onRestoreInstanceState(Bundle savedInstanceState) { 178 super.onRestoreInstanceState(savedInstanceState); 179 int year = savedInstanceState.getInt(YEAR); 180 int month = savedInstanceState.getInt(MONTH); 181 int day = savedInstanceState.getInt(DAY); 182 mDatePicker.init(year, month, day, this); 183 updateTitle(year, month, day); 184 } 185} 186