Android DatePicker Demonstration

Screenshot of the app running in the Gingerbread emulator
Note the way that the set, before and add methods of the Calendar object supports the use of the DatePicker object. You can compare this code with the RemObjects C# version below it.
The code of MainActivity.pas
namespace org.me.android_datepicker; interface uses java.util, android.app, android.content, android.os, android.util, android.view, android.widget; type MainActivity = public class(Activity) private dp : DatePicker; tv : TextView; public method onCreate(savedInstanceState: Bundle); override; method DateChange(v : View; year: Integer; monthOfYear: Integer; dayOfMonth: Integer); end; implementation method MainActivity.DateChange(v : View; year, monthOfYear, dayOfMonth: Integer); var DaysUntil : Integer := 0; calToday, calDP_Date : Calendar; begin calToday := Calendar.getInstance; calDP_Date := Calendar.getInstance; //Set date and time of target date to midnight on date of DatePicker. calDP_Date.set(year, monthOfYear, dayOfMonth, 0, 0, 0); while calToday.before(calDP_Date) do begin calToday.add(Calendar.DAY_OF_MONTH, 1); inc(DaysUntil); end; //Output the number of days we are before the target. tv.Text := 'Days to wait: ' + DaysUntil.toString; end; method MainActivity.onCreate(savedInstanceState: Bundle); var DaysUntil : Integer := 0; odcListener : DatePicker.OnDateChangedListener; begin inherited; // Set view from the "main" layout resource. ContentView := R.layout.main; //Assign DatePicker and TextView. dp := DatePicker(findViewById(R.id.dp1)); tv := TextView(findViewById(R.id.tv1)); //Assign OnDateChangedListener. odcListener := new interface DatePicker.OnDateChangedListener(onDateChanged := @DateChange); //Set to date of DatePicker to Christmas Eve, 2012. dp.init(2012, 11, 24, odcListener); end; end.
Layout File
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal"> <DatePicker android:id="@+id/dp1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv1" android:textSize="18sp" android:textColor="#00FF00" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/dp1" android:text="@string/instruction" /> </RelativeLayout>
Strings File
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Date Picker</string> <string name="instruction">Change target date to see days to wait.</string> </resources>
RemObjects C# Version of Android DatePicker Demonstration
using java.util;
using android.app;
using android.content;
using android.os;
using android.util;
using android.view;
using android.widget;
namespace org.me.android_datepicker_cs
{
public class MainActivity: Activity
{
private DatePicker dp;
private TextView tv;
public void DateChange(View v, Integer year, Integer monthOfYear,Integer dayOfMonth)
{
Integer DaysUntil = 0;
Calendar calToday, calDP_Date;
calToday = Calendar.getInstance();
calDP_Date = Calendar.getInstance();
//Set date and time of target date to midnight on date of DatePicker.
calDP_Date.set(year, monthOfYear, dayOfMonth, 0, 0, 0);
while (calToday.before(calDP_Date))
{
calToday.add(Calendar.DAY_OF_MONTH, 1);
inc(DaysUntil);
}
//Output the number of days we are before the target.
tv.Text = "Days to wait: " + DaysUntil.toString();
}
public override void onCreate(Bundle savedInstanceState)
{
base.onCreate(savedInstanceState);
// Set our view from the "main" layout resource
ContentView = R.layout.main;
//Assign DatePicker and TextView.
dp = (DatePicker)(findViewById(R.id.dp1));
tv = (TextView)(findViewById(R.id.tv1));
//Set to date of DatePicker to Christmas Eve, 2014.
dp.init(2014, 11, 24, DateChange);
}
}
}