Code for open date picker dialog in android.

Total Post:14

Points:98
 562  View(s)
Ratings:
Rate this:

Code for open date picker dialog(Calendar) in android after clicking on EditText.

  1. Post:14

    Points:98
    Re: Code for open date picker dialog in android.

    /* 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();

                }

            });

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!