70

I'm developing an app that requires users to select dates (dd/mm/yyyy). I want to show a dialog box with DatePicker on button click. once date is selected it must show in EditText.

I'm using Android Studio 2.2, the project with min sdk is 23.

Kindly help me to do the required code.

Md Tariqul Islam
  • 2,736
  • 1
  • 20
  • 35
Pratik Pitale
  • 1,655
  • 1
  • 16
  • 30
  • 2
    Hi, you can try to this link it can help you http://stackoverflow.com/questions/14933330/datepicker-how-to-popup-datepicker-when-click-on-edittext – Dileep Patel Oct 07 '16 at 11:35
  • 2
    Possible duplicate of [Datepicker: How to popup datepicker when click on edittext](http://stackoverflow.com/questions/14933330/datepicker-how-to-popup-datepicker-when-click-on-edittext) – poring91 Mar 02 '17 at 07:22
  • http://blog.nkdroidsolutions.com/android-custom-date-picker-dialog-example/ – duggu Dec 19 '17 at 10:38

4 Answers4

80

enter image description here

I. In your build.gradle add latest appcompat library, at the time 24.2.1

dependencies {  
    compile 'com.android.support:appcompat-v7:X.X.X' 
    // where X.X.X version
}

II. Make your activity extend android.support.v7.app.AppCompatActivity and implement the DatePickerDialog.OnDateSetListener interface.

public class MainActivity extends AppCompatActivity  
    implements DatePickerDialog.OnDateSetListener {

III. Create your DatePickerDialog setting a context, the implementation of the listener and the start year, month and day of the date picker.

DatePickerDialog datePickerDialog = new DatePickerDialog(  
    context, MainActivity.this, startYear, starthMonth, startDay);

IV. Show your dialog on the click event listener of your button

((Button) findViewById(R.id.myButton))
    .setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        datePickerDialog.show();
    }
});
saulmm
  • 2,398
  • 15
  • 21
45
    final Calendar newCalendar = Calendar.getInstance();
    final DatePickerDialog  StartTime = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    Calendar newDate = Calendar.getInstance();
                    newDate.set(year, monthOfYear, dayOfMonth);
                    activitydate.setText(dateFormatter.format(newDate.getTime()));
                }
    
            }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
    
      btn_checkin.setOnClickListener(new View.OnClickListener() {
@Override   public void onClick(View v) {
         StartTime.show():    
     });
Nemesius
  • 179
  • 10
M.Yogeshwaran
  • 1,419
  • 4
  • 22
  • 48
18

it works for me. if you want to enable future time for choose, you have to delete maximum date. You need to to do like followings.

 btnDate.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
          DialogFragment newFragment = new DatePickerFragment();
              newFragment.show(getSupportFragmentManager(), "datePicker");
          }
     });
            
     public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
         @Override
         public Dialog onCreateDialog(Bundle savedInstanceState) {
              final Calendar c = Calendar.getInstance();
              int year = c.get(Calendar.YEAR);
              int month = c.get(Calendar.MONTH);
              int day = c.get(Calendar.DAY_OF_MONTH);
              DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
              dialog.getDatePicker().setMaxDate(c.getTimeInMillis());
              return  dialog;
         }
            
         public void onDateSet(DatePicker view, int year, int month, int day) {
              btnDate.setText(ConverterDate.ConvertDate(year, month + 1, day));
         }
     }
 }
Hakan SONMEZ
  • 2,176
  • 2
  • 21
  • 32
Volkan Sonmez
  • 745
  • 3
  • 14
9

Following code works..

datePickerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(0);
        }
    });

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
    return new DatePickerDialog(this, datePickerListener, year, month, day);
}

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int selectedYear,
                          int selectedMonth, int selectedDay) {
        day = selectedDay;
        month = selectedMonth;
        year = selectedYear;
        datePickerButton.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
                + selectedYear);
    }
};
Pratik Pitale
  • 1,655
  • 1
  • 16
  • 30