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