Tufan
Tufan

Reputation: 2839

Custom DatePicker On EditText

I have 2 EditText.

When anyone click on a EditText datepicker is displayed to set date in EditText. so my coding for EditText is

    sdate.setOnTouchListener( new DrawableClickListener.RightDrawableClickListener(sdate)
                {
                    @Override
                    public boolean onDrawableClick()
                    {
                         MyDatePickerDialog(sdate);// put edit text view. 
                          return true;                     
                    }                   
                } );
                edate.setOnTouchListener( new DrawableClickListener.RightDrawableClickListener(edate)
                {
                    @Override
                    public boolean onDrawableClick()
                    {
                         MyDatePickerDialog(edate);// put edit text view. 
                         return true;                   
                    }
                } );

And my custom datepicker is

    private void MyDatePickerDialog(final EditText sdate) {
        Toast.makeText(getApplicationContext(),"sdgfdg" , Toast.LENGTH_LONG).show();
        DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }

            private void updateLabel() {
                 String myFormat = "MM/dd/yy"; //In which you need put here
                    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                    sdate.setText(sdf.format(myCalendar.getTime()));
            }

        };

    }

But the problem is toast message is displayed but it dosent display datepicker

Upvotes: 2

Views: 602

Answers (2)

Piyush
Piyush

Reputation: 18933

You have a custom method only and you need to create a listener for OnDateSetListener to pass your DatePickerDialog

 private void MyDatePickerDialog(final EditText sdate) {
    Toast.makeText(getApplicationContext(),"sdgfdg" , Toast.LENGTH_LONG).show();
     Calendar newCalendar = Calendar.getInstance();
     DatePickerDialog myPickerDialog = new DatePickerDialog(this, new OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            updateLabel();
        }

    },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

        private void updateLabel() {
             String myFormat = "MM/dd/yy"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                sdate.setText(sdf.format(newCalendar.getTime()));
        }

    myPickerDialog.show();

  }

NOTE: You can use onClickListener instead of onTouchListener.

Upvotes: 2

Kirankumar Zinzuvadia
Kirankumar Zinzuvadia

Reputation: 1249

You have only created Listener of DatePickerDialog, not DatePickerDialog itself.

You have to do this:

DatePickerDialog datePickerDialog = new DatePickerDialog(
                getApplicationContext(), your Listener, year, month, dayOfMonth);
        datePickerDialog.show();

Upvotes: 1

Related Questions