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