Date picker in Android
public class dateresult extends Activity
{
private TextView tvdisplaydate;
private DatePicker dpResult;
private Button bntchangedate;
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 999;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
setCurrentdateonView();
addListenerOnButton();
}
public void setCurrentdateonView(){
tvdisplaydate = (TextView)findViewById(R.id.tvdate);
dpResult = (DatePicker) findViewById(R.id.dpResult);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH) ;
day = c.get(Calendar.DAY_OF_MONTH);
tvdisplaydate.setText(new StringBuffer() .append(month+1).append("-").append(day).append("-").append(year).append(""));
dpResult.init(year, month, day, null);
}
public void addListenerOnButton(){
bntchangedate = (Button)findViewById(R.id.bntchangedate);
bntchangedate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch(id){
case DATE_DIALOG_ID:
return new DatePickerDialog(this,datePickerLisner,year,month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerLisner = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int Selectyear,int Selectmonth, int Selectday) {
year= Selectyear;
month= Selectmonth;
day = Selectday;
tvdisplaydate.setText(new StringBuilder()
.append(Selectmonth+1).append("-").append(Selectday).append("-").append(Selectyear).append(""));
dpResult.init(year, month, day, null);
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bntchangedate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Date" />
<TextView
android:id="@+id/lbldate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (M-D-YYYY) :"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/tvdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="@+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Use the DatePicker
http://developer.android.com/reference/android/widget/DatePicker.html
It is availible since API Level 1
Here a example how to use the DatePickerDialog.
First add a TextView and a Button to your layout.xml
<Button android:id="@+id/myDatePickerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Date"/>
<TextView android:id="@+id/showMyDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
Next you have to initialize the Button and TextView in the onCreate Method of your layout. You need this class variables
private int mYear;
private int mMonth;
private int mDay;
private TextView mDateDisplay;
private Button mPickDate;
static final int DATE_DIALOG_ID = 0;
Here the onCreate method
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDateDisplay = (TextView) findViewById(R.id.showMyDate);
mPickDate = (Button) findViewById(R.id.myDatePickerButton);
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date
updateDisplay();
}
UpdateDisplay method:
private void updateDisplay() {
this.mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
The callback listener for the DatePickDialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
The onCreateDialog method, called by showDialog()
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
Hope it helps, used it and it works fine.
Example from
http://developer.android.com/guide/tutorials/views/hello-datepicker.html
Here is an updated version which documents backwards compatibility with the Support Library:
http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker