You can use “
android.widget.DatePicker”
class to render a date picker component to select day, month and year
in a pre-defined user interface in android
In this tutorial, you'll create a
DatePickerDialog,
which presents the date picker in a floating dialog box at the press
of a button. When the date is set by the user, a
TextView
will update with the new date.
xml(date_picker_display.xml)
<?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:layout_gravity="center_horizontal" >
<TextView
android:id="@+id/Date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Selected Date "
android:textSize="30sp" />
<TextView
android:id="@+id/displayDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Date"
android:text="Date will appear Here"
android:textSize="30sp" />
<Button
android:id="@+id/pickDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/displayDate"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Pick a date using Date Picker"
/>
</RelativeLayout>
Activity(DatePickerDisplay.class)
package com.androidsurya.datepicker;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
public class DatePickerDisplay extends Activity {
private TextView pDisplayDate;
private Button pPickDate;
private int Year;
private int Month;
private int Day;
/**
* This integer will uniquely define the dialog to be used for
displaying
* date picker.
*/
static final int
DATE_DIALOG_ID = 007;
/**
Callback received when the user "picks" a
date in the dialog */
private DatePickerDialog.OnDateSetListener pDateSetListener = new
DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int
monthOfYear,
int dayOfMonth) {
Year = year;
Month = monthOfYear;
Day = dayOfMonth;
updateDisplay();
displayToast();
}
};
/** Updates the date in the TextView */
private void updateDisplay() {
pDisplayDate.setText(new StringBuilder()
// Month is 0 based so add 1
.append(Month + 1).append("/").append(Day).append("/")
.append(Year).append(" "));
}
/** Displays a notification when the date is updated */
private void displayToast() {
Toast.
makeText(
this,
new StringBuilder().append("Date choosen is ").append(
pDisplayDate.getText()), Toast.
LENGTH_SHORT).show();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
date_picker_display);
/** Capture our View elements */
pDisplayDate = (TextView) findViewById(R.id.
displayDate);
pPickDate = (Button) findViewById(R.id.
pickDate);
/** Listener for click event of the button */
pPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(
DATE_DIALOG_ID);
}
});
/** Get the current date */
final Calendar cal = Calendar.
getInstance();
Year = cal.get(Calendar.
YEAR);
Month = cal.get(Calendar.
MONTH);
Day = cal.get(Calendar.
DAY_OF_MONTH);
/** Display the current date in the TextView */
updateDisplay();
}
/** Create a new dialog for date picker */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case
DATE_DIALOG_ID:
return new DatePickerDialog(this, pDateSetListener, Year,
Month,
Day);
}
return null;
}
}
Register Activity in Android Manifest file(AndroidManifest.xml)
<activity
android:name=
"com.androidsurya.datepicker.DatePickerDisplay"
android:label=
"@string/app_name" >
Output Screenshort
For More information about Date Picker : Android Developers site