Android: DatePickerDialog on EditText Click Event

In this Android Example, we will see how to open a DatePickerDialog on EditText click event, select the date from the Calendar, and set it in EditText in dd-MM-yyyy format using SimpleDateFormat.

Download “Show DataPickerDialog EditText Click Event” DatePickerDialogEditTextClickEvent.zip – Downloaded 5028 times – 1 MB

Android Project

Create a new Android project and name it as DatePickerDialogEditTextClickEvent.

Resources

Layout Files

activity_main.xml

This XML layout file (activity_main.xml) is used for defining EditText. Open activity_main.xml file in res/layout and copy the following content.

strings.xml

Open res/values/strings.xml and replace it with following content.

Source files

Activity Class

Open your activity class (MainActivity.java) and copy the following code.

  • We create an instance for SimpleDateFormat to display the date in “dd-MM-yyyy” format.
    dateFormatter = new SimpleDateFormat(“dd-MM-yyyy”, Locale.US);
  • The following code sets on click listener for EditText.

  • We declare two instances of DatePickerDialog – fromDatePickerDialog, toDatePickerDialog – by using the constructor DatePickerDialog(Context context, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth).
  • We implement the DatePickerDialog.OnDateSetListener interface to receive a callback when the user sets the date. In the onDateSet() callback we set the date selected by the user in the EditText (using SimpleDateFormat “dd-MM-yyyy” format).

  • We display the date picker dialog by calling show() method on dialog instance in onClick listener of EditText.

Output

android-datepickerdialog-output-1

android-datepickerdialog-output-2

  • Bishal

    saved my time. Thanks

  • http://my.opera.com/sufian88/blog/ Sufian

    Nice solution but it doesn’t work if the

    wasn’t focused. To fix this, I added a OnFocusChangeListener like following :

    • http://qaleps.tumblr.com Alejandro Lagos

      Cool, thanks a lot.
      do you know how to set a max date for the DatePickerDialog?

      • http://my.opera.com/sufian88/blog/ Sufian

        DatePicker‘s setMaxDate() will help you for API 11 and above.

        For pre-11, check this SO answer.

    • http://my.opera.com/sufian88/blog/ Sufian

      Updated. Was having trouble when my Activity was recreated/restored.

    • Madhu

      Simple Solution. works awesome:)

  • Sean Baergen

    What is the licence on this code?

  • Harshad

    code is right…
    but when I am changing fromdate next time without changing todate ,month of todate gets incremented by 12 .At same time when I change both fromdate and todate things work properly
    Whats the problem?????
    Please rply asap………….

  • http://themasterworld.com/ The Master World

    Edit text in android studio
    if you are beginner and how to create text view in android then this lecture can help a lot
    http://themasterworld.com/edit-text-in-android-studio/

  • http://themasterworld.com/ The Master World

    Edit text in android studio
    if you are beginner and do not how to create a text fied in android then this lecture can help a lot
    http://themasterworld.com/edit-text-in-android-studio/

  • http://themasterworld.com/ The Master World

    Button and its click events in android
    if you are beginner and do not know how create Button and its click events in android
    http://themasterworld.com/android-button-help-to-create-button/

  • VIJAY T

    hi Thanks

    I need both date and time picker.it would be cery helpful to me

  • Krishna Chandramouli

    excellent article, thanks for the tutorial.

  • Derrick Njenga

    Awesome tut. Helped me alot.
    Thanks

  • Gokul G

    how to validate end date greater than start date .

  • android developer

    Nice tutorial but how can i select only month and year without date

  • abderrahim zaghouri

    Nice it’s helpful . thanks a lot =D

  • fayzal

    thank you, very helpful

  • Rakshit

    To avoid the keyboard popup along with the datepicker.
    editText_input_date.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    int inType = editText_input_date.getInputType(); // backup the input type
    editText_input_date.setInputType(InputType.TYPE_NULL); // disable soft input
    editText_input_date.onTouchEvent(event); // call native handler
    editText_input_date.setInputType(inType); // restore input type
    return true;
    }
    });