Sure, here is the code for open date picker dialogue in Android:
Code snippet
public void openDatePickerDialog() {
// Create a date picker dialog
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
// Set the date in the text field
editTextDate.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
}
},
Calendar.getInstance().get(Calendar.YEAR),
Calendar.getInstance().get(Calendar.MONTH),
Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
// Show the date picker dialog
datePickerDialog.show();
}
This code will first create a DatePickerDialog object. The
DatePickerDialog object will be used to display a date picker dialog to the user.
The DatePickerDialog object has three parameters:
The context of the activity where the dialog will be displayed.
A listener that will be called when the user selects a date.
The initial date that will be displayed in the date picker dialog.
The onDateSet() method of the listener will be called when the user selects a date. The
onDateSet() method has three parameters:
The DatePicker object that was used to select the date.
The year that was selected.
The month that was selected.
The day of the month that was selected.
In the onDateSet() method, you can set the date in the text field by calling the
setText() method of the text field.
To use this code, you would need to call the openDatePickerDialog() method in your app. For example, you could call the method in your app's
onCreate() method.
Here is an example of how you could call the openDatePickerDialog() method in your app's
onCreate() method:
Code snippet
public void onCreate() {
super.onCreate();
// Create a text field to display the date
editTextDate = findViewById(R.id.editTextDate);
// Open the date picker dialog
openDatePickerDialog();
}
When you run the app, you will see a date picker dialog. You can select a date in the date picker dialog and the date will be displayed in the text field.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is the code for open date picker dialogue in Android:
Code snippet
This code will first create a
DatePickerDialogobject. TheDatePickerDialogobject will be used to display a date picker dialog to the user.The
DatePickerDialogobject has three parameters:The
onDateSet()method of the listener will be called when the user selects a date. TheonDateSet()method has three parameters:DatePickerobject that was used to select the date.In the
onDateSet()method, you can set the date in the text field by calling thesetText()method of the text field.To use this code, you would need to call the
openDatePickerDialog()method in your app. For example, you could call the method in your app'sonCreate()method.Here is an example of how you could call the
openDatePickerDialog()method in your app'sonCreate()method:Code snippet
When you run the app, you will see a date picker dialog. You can select a date in the date picker dialog and the date will be displayed in the text field.
/* get Instance of calendar */
Calendar myCalendar = Calendar.getInstance();
/* Create object of EditText. */
EditText dateOfBirth = (EditText) findViewById(R.id.dob);
/* Set value on date objects*/
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
private void updateLabel() {
String myFormat = "yyyy-MM-dd"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
dateOfBirth.setText(sdf.format(myCalendar.getTime()));
}
/* open Date Picker Dialog for dob*/
dateOfBirth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DatePickerDialog(EditProfileActivity.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});