Search code examples
javaandroidandroid-datepicker

autoclose DatePickerDialog


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?


Solution

  • Create a MyPicker class :)

        import static yourPackageName.MainActivity.sdf; // get static imports
    
    
        public class MyPicker extends DatePickerDialog {
    
        Context context;
        @Override
        public DatePicker getDatePicker() {
            return super.getDatePicker();
        }
    
    
        public MyPicker(Context context, int themeResId, OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) {
            super(context, themeResId, listener, year, monthOfYear, dayOfMonth);
            this.context = context;
        }
    
        @Override
        public void onDateChanged(DatePicker view, int year, int month, int dayOfMonth) {
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, month);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            datePicker.setText(sdf.format(myCalendar.getTime()));
            datePickerDialog.dismiss();
        }
    }
    

    now assuming your main class in MainActivity

    public class MainActivity extends AppCompatActivity {
    
    
        public static Calendar myCalendar = Calendar.getInstance();
    
    
        public static String myFormat = "MM/dd/yy";
        public static SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        public static EditText datePicker;
        public static MyPicker datePickerDialog;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            datePicker = (EditText) findViewById(R.id.popUpDate);
            datePicker.setText(sdf.format(myCalendar.getTime()));
    
            datePicker.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    datePickerDialog = new MyPicker(MainActivity.this, R.style.AppTheme_Dialog, new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        }
                    }, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
                    datePickerDialog.show();
                }
    
            });
        }
    
    }
    }
    

    or if you don't need to apply styles

    public class MainActivity extends AppCompatActivity {
    
        public  String myFormat = "MM/dd/yy";
        public  SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        public  EditText datePicker;
        final Calendar calendar = Calendar.getInstance();
        private DatePickerDialog datePickerDialog;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            datePicker = (EditText) findViewById(R.id.popUpDate);
            datePicker.setText(sdf.format(calendar.getTime()));
    
            datePicker.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    datePickerDialog = new DatePickerDialog(MainActivity.this, null, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                    datePickerDialog.getDatePicker().init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
                        @Override
                        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            calendar.set(Calendar.YEAR, year);
                            calendar.set(Calendar.MONTH, monthOfYear);
                            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                            datePicker.setText(sdf.format(calendar.getTime()));
                            datePickerDialog.dismiss();
                        }
                    });
                    datePickerDialog.show();
                }
            });
        }
    }
    

    Works like a charm!