• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DIALOG_ID = 0;
55     static final int DATE_DIALOG_ID = 1;
56 
57     @Override
onCreate(Bundle savedInstanceState)58     protected void onCreate(Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60 
61         setContentView(R.layout.date_widgets_example_1);
62 
63         mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
64 
65         Button pickDate = (Button) findViewById(R.id.pickDate);
66         pickDate.setOnClickListener(new View.OnClickListener() {
67 
68             public void onClick(View v) {
69                 showDialog(DATE_DIALOG_ID);
70             }
71         });
72 
73         Button pickTime = (Button) findViewById(R.id.pickTime);
74         pickTime.setOnClickListener(new View.OnClickListener() {
75 
76             public void onClick(View v) {
77                 showDialog(TIME_DIALOG_ID);
78             }
79         });
80 
81         final Calendar c = Calendar.getInstance();
82         mYear = c.get(Calendar.YEAR);
83         mMonth = c.get(Calendar.MONTH);
84         mDay = c.get(Calendar.DAY_OF_MONTH);
85         mHour = c.get(Calendar.HOUR_OF_DAY);
86         mMinute = c.get(Calendar.MINUTE);
87 
88         updateDisplay();
89     }
90 
91     @Override
onCreateDialog(int id)92     protected Dialog onCreateDialog(int id) {
93         switch (id) {
94             case TIME_DIALOG_ID:
95                 return new TimePickerDialog(this,
96                         mTimeSetListener, mHour, mMinute, false);
97             case DATE_DIALOG_ID:
98                 return new DatePickerDialog(this,
99                             mDateSetListener,
100                             mYear, mMonth, mDay);
101         }
102         return null;
103     }
104 
105     @Override
onPrepareDialog(int id, Dialog dialog)106     protected void onPrepareDialog(int id, Dialog dialog) {
107         switch (id) {
108             case TIME_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