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 17 package com.example.android.apis.view; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.app.DatePickerDialog; 23 import android.app.TimePickerDialog; 24 import android.app.Dialog; 25 import android.os.Bundle; 26 import android.widget.TextView; 27 import android.widget.Button; 28 import android.widget.DatePicker; 29 import android.widget.TimePicker; 30 import android.view.View; 31 32 import java.util.Calendar; 33 34 /** 35 * Basic example of using date and time widgets, including 36 * {@link android.app.TimePickerDialog} and {@link android.widget.DatePicker}. 37 * 38 * Also provides a good example of using {@link Activity#onCreateDialog}, 39 * {@link Activity#onPrepareDialog} and {@link Activity#showDialog} to have the 40 * activity automatically save and restore the state of the dialogs. 41 */ 42 public class DateWidgets1 extends Activity { 43 44 // where we display the selected date and time 45 private TextView mDateDisplay; 46 47 // date and time 48 private int mYear; 49 private int mMonth; 50 private int mDay; 51 private int mHour; 52 private int mMinute; 53 54 static final int TIME_12_DIALOG_ID = 0; 55 static final int TIME_24_DIALOG_ID = 1; 56 static final int DATE_DIALOG_ID = 2; 57 58 @Override onCreate(Bundle savedInstanceState)59 protected void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 62 setContentView(R.layout.date_widgets_example_1); 63 64 mDateDisplay = (TextView) findViewById(R.id.dateDisplay); 65 66 setDialogOnClickListener(R.id.pickDate, DATE_DIALOG_ID); 67 setDialogOnClickListener(R.id.pickTime12, TIME_12_DIALOG_ID); 68 setDialogOnClickListener(R.id.pickTime24, TIME_24_DIALOG_ID); 69 70 final Calendar c = Calendar.getInstance(); 71 mYear = c.get(Calendar.YEAR); 72 mMonth = c.get(Calendar.MONTH); 73 mDay = c.get(Calendar.DAY_OF_MONTH); 74 mHour = c.get(Calendar.HOUR_OF_DAY); 75 mMinute = c.get(Calendar.MINUTE); 76 77 updateDisplay(); 78 } 79 setDialogOnClickListener(int buttonId, final int dialogId)80 private void setDialogOnClickListener(int buttonId, final int dialogId) { 81 Button b = (Button) findViewById(buttonId); 82 b.setOnClickListener(new View.OnClickListener() { 83 public void onClick(View v) { 84 showDialog(dialogId); 85 } 86 }); 87 } 88 89 @Override onCreateDialog(int id)90 protected Dialog onCreateDialog(int id) { 91 switch (id) { 92 case TIME_12_DIALOG_ID: 93 case TIME_24_DIALOG_ID: 94 return new TimePickerDialog(this, 95 mTimeSetListener, mHour, mMinute, id == TIME_24_DIALOG_ID); 96 case DATE_DIALOG_ID: 97 return new DatePickerDialog(this, 98 mDateSetListener, 99 mYear, mMonth, mDay); 100 } 101 return null; 102 } 103 104 @Override onPrepareDialog(int id, Dialog dialog)105 protected void onPrepareDialog(int id, Dialog dialog) { 106 switch (id) { 107 case TIME_12_DIALOG_ID: 108 case TIME_24_DIALOG_ID: 109 ((TimePickerDialog) dialog).updateTime(mHour, mMinute); 110 break; 111 case DATE_DIALOG_ID: 112 ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay); 113 break; 114 } 115 } 116 updateDisplay()117 private void updateDisplay() { 118 mDateDisplay.setText( 119 new StringBuilder() 120 // Month is 0 based so add 1 121 .append(mMonth + 1).append("-") 122 .append(mDay).append("-") 123 .append(mYear).append(" ") 124 .append(pad(mHour)).append(":") 125 .append(pad(mMinute))); 126 } 127 128 private DatePickerDialog.OnDateSetListener mDateSetListener = 129 new DatePickerDialog.OnDateSetListener() { 130 131 public void onDateSet(DatePicker view, int year, int monthOfYear, 132 int dayOfMonth) { 133 mYear = year; 134 mMonth = monthOfYear; 135 mDay = dayOfMonth; 136 updateDisplay(); 137 } 138 }; 139 140 private TimePickerDialog.OnTimeSetListener mTimeSetListener = 141 new TimePickerDialog.OnTimeSetListener() { 142 143 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 144 mHour = hourOfDay; 145 mMinute = minute; 146 updateDisplay(); 147 } 148 }; 149 pad(int c)150 private static String pad(int c) { 151 if (c >= 10) 152 return String.valueOf(c); 153 else 154 return "0" + String.valueOf(c); 155 } 156 } 157