Jeeten Parmar
Jeeten Parmar

Reputation: 5757

DatePickerDialog opens more than one time when touches EditText

I am using DatePickerDialog in my application. It is Fragment. Now, Issue is that when I couch on EditText, It open dialog 3 times.

I am using Total 2 DatePickerDialog for 2 different EditText and 2 TimePickerDialog for 2 different EditText here.

My Code :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

((EditText) etIOStartDate).setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub

        new DatePickerDialog(getActivity(), StartDate, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();
    return false;
    }
});
}
DatePickerDialog.OnDateSetListener StartDate = 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);

            String myFormat = "dd-MM-yyyy";
            SimpleDateFormat sdf = new SimpleDateFormat(myFormat,
                    Locale.getDefault());

            ((EditText) etIOStartDate)
                    .setText(sdf.format(myCalendar.getTime()));
        }
    };

Please help me to solve the problem.

Upvotes: 0

Views: 1692

Answers (4)

Finder
Finder

Reputation: 1217

you have to maintain one global variable to check is your datepicker is opened or not. When you open datepicker assign that variable to true.

boolean isDatePickerOpened=false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

((EditText) etIOStartDate).setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub

if(!isDatePickerOpened){

   isDatePickerOpened=true;
        new DatePickerDialog(getActivity(), StartDate, myCalendar.get(Calendar.YEAR),       myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
    return false;
    }
});
}


DatePickerDialog.OnDateSetListener StartDate = 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);

            String myFormat = "dd-MM-yyyy";
            SimpleDateFormat sdf = new SimpleDateFormat(myFormat,
                    Locale.getDefault());

            ((EditText) etIOStartDate)
                    .setText(sdf.format(myCalendar.getTime()));

                isDatePickerOpened=false;

        }
    };

when date picker dialog dismissed you have to set that isDatePickerOpened to false. This may help you..

Upvotes: 1

Tushar Narang
Tushar Narang

Reputation: 1957

Use this under onCreate()

txtDate = (EditText) findViewById(R.id.etSdate);
txtDate.setOnClickListener(new View.OnClickListener() 
{
        @Override
        public void onClick(View v) 
        {
            etSdate();
        }

});

call this function in end of your program

public void etSdate()
   {
       {

            // Process to get Current Date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);

            // Launch Date Picker Dialog
            DatePickerDialog dpd = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                int monthOfYear, int dayOfMonth) {
                            // Display Selected date in textbox
                            txtDate.setText(dayOfMonth + "-"
                                    + (monthOfYear + 1) + "-" + year);

                        }
                    }, mYear, mMonth, mDay);
            dpd.show();


  }
}

Upvotes: 0

Ando Masahashi
Ando Masahashi

Reputation: 3122

just setOnClickListener to EditText

you have used setOnTouchListener so it finds 2-3 times touch so it will open

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

Try this. There are two events Action down and Action up.

((EditText) etIOStartDate).setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            switch(arg1.getAction())
            {
            case MotionEvent.ACTION_DOWN :
                new DatePickerDialog(getActivity(), StartDate, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                break;
            case MotionEvent.ACTION_UP  :
                break;

            }

            return true;
        }
    });

Upvotes: 4

Related Questions