I am using the following code to create a DatePickerDialog
from an EditText
.
import android.widget.DatePicker;
import android.app.DatePickerDialog;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
Calendar myCalendar = Calendar.getInstance();
String myFormat = "MM/dd/yy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
@Override
protected void onCreate(Bundle savedInstanceState) {
EditText datePicker = (EditText) findViewById(R.id.popUpDate);
datePicker.setText(sdf.format(myCalendar.getTime()));
datePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(AddAccount.this, R.style.AppTheme_Dialog, 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);
datePicker.setText(sdf.format(myCalendar.getTime()));
}
}, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
Is it possible to have the default one automatically close after a date is chosen? Or would that require a complete remake of the datepicker
widget?
Create a MyPicker class :)
now assuming your main class in MainActivity
or if you don't need to apply styles
Works like a charm!