Thursday, November 5, 2009

DatePicker Widget

I've finally started working on my first app. I needed to use a DatePicker but didn't want to use a dialog, I wanted the DatePicker to appear on the current page. Here is my code:


/**
* DatePickerActivity
*
*
*/
public class DatePickerActivity extends Activity {

private DatePicker datePicker;

private int year;
private int month;
private int day;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

////////////////////////////////////////////////////////////
// Get access to Date Picker
datePicker = (DatePicker)findViewById(R.id.DatePicker01);

Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);

datePicker.init(year, month, day, dateSetListener);
}


/**
* DatePicker.OnDateChangedListener
*
*/
private DatePicker.OnDateChangedListener dateSetListener
= new DatePicker.OnDateChangedListener() {

/*
* onDateChanged
*/
public void onDateChanged(DatePicker view, int newYear,
int newMonth, int newDay) {

String dateOutput = String.format("Date Selected: %02d/%02d/%04d",
newDay, newMonth, newYear);
Log.i("Debug", dateOutput);
}
};
}

No comments:

Post a Comment