Here is the complete code for that..
Button bt_setdate=(Button) findViewById(R.id.bt_setdate);
bt_setdate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
// get the current date
}
});
and outside onCreate()
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
//Edit------------------------------------------//
final Calendar c = Calendar.getInstance();
Year = c.get(Calendar.YEAR);
Month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
//Edit------------------------------------------//
return new DatePickerDialog(this, datePickerListener,
year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
String selmonth=cm.getMonth(month);
bookingdate=String.valueOf(year)+"-"+String.valueOf(month)+"-"+String.valueOf(day);
}
};
bookingdate
contains the date that is selected..
and add this to your layout
<DatePicker
android:id="@+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>