7 package com.google.appinventor.components.runtime;
9 import android.app.DatePickerDialog;
10 import android.os.Handler;
23 import java.text.DateFormatSymbols;
24 import java.util.Calendar;
25 import java.util.GregorianCalendar;
39 @DesignerComponent(version = YaVersion.DATEPICKER_COMPONENT_VERSION,
40 category = ComponentCategory.USERINTERFACE,
41 description =
"<p>A button that, when clicked on, launches a popup" +
42 " dialog to allow the user to select a date.</p>")
46 private DatePickerDialog date;
48 private int year, month, javaMonth, day;
49 private Calendar instant;
50 private String [] localizedMonths =
new DateFormatSymbols().getMonths();
51 private boolean customDate =
false;
53 private Handler androidUIHandler;
63 final Calendar c = Calendar.getInstance();
64 year = c.get(Calendar.YEAR);
65 javaMonth = c.get(Calendar.MONTH);
66 month = javaMonth + 1;
67 day = c.get(Calendar.DAY_OF_MONTH);
69 date =
new DatePickerDialog(this.container.
$context(), datePickerListener, year, javaMonth,
72 androidUIHandler =
new Handler();
80 @
SimpleProperty(description =
"the Year that was last picked using the DatePicker",
90 @
SimpleProperty(description =
"the number of the Month that was last picked using the " +
91 "DatePicker. Note that months start in 1 = January, 12 = December.",
101 @
SimpleProperty(description =
"Returns the name of the Month that was last picked using the " +
102 "DatePicker, in textual format.",
105 return localizedMonths[javaMonth];
112 @
SimpleProperty(description =
"the Day of the month that was last picked using the DatePicker.",
122 @
SimpleProperty(description =
"the instant of the date that was last picked using the DatePicker.",
128 @
SimpleFunction(description =
"Allows the user to set the date to be displayed when the date picker opens.\n" +
129 "Valid values for the month field are 1-12 and 1-31 for the day field.\n")
131 int jMonth = month - 1;
133 GregorianCalendar cal =
new GregorianCalendar(year, jMonth, day);
134 cal.setLenient(
false);
136 }
catch (java.lang.IllegalArgumentException e) {
139 date.updateDate(year, jMonth, day);
144 @
SimpleFunction(description =
"Allows the user to set the date from the instant to be displayed when the date picker opens.")
149 date.updateDate(year, month, day);
169 Calendar c = Calendar.getInstance();
170 int year = c.get(Calendar.YEAR);
171 int jMonth = c.get(Calendar.MONTH);
172 int day = c.get(Calendar.DAY_OF_MONTH);
173 date.updateDate(year, jMonth, day);
184 private DatePickerDialog.OnDateSetListener datePickerListener =
185 new DatePickerDialog.OnDateSetListener() {
187 public void onDateSet(android.widget.DatePicker datePicker,
int selectedYear,
188 int selectedMonth,
int selectedDay) {
189 if (datePicker.isShown()) {
191 javaMonth = selectedMonth;
192 month = javaMonth + 1;
194 date.updateDate(year, javaMonth, day);
200 androidUIHandler.post(
new Runnable() {
212 @SimpleEvent(description =
"Event that runs after the user chooses a Date in the dialog")