Archie.bpgc
Archie.bpgc

Reputation: 24012

Why is the date dialog asking to click thrice to dismiss

public class QuickIWantActivity extends Activity implements
        OnDateSetListener {
.....
.....
.....

dateET.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {

Calendar cal = Calendar.getInstance();
DatePickerDialog datePickDiag = new DatePickerDialog(
        QuickIWantActivity.this, QuickIWantActivity.this, cal
            .get(Calendar.YEAR), cal.get(Calendar.MONTH),
        cal.get(Calendar.DAY_OF_MONTH));
        datePickDiag.show();
        return true;
        }
    });

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

    int month = monthOfYear + 1;
    dateET.setText(dayOfMonth + "- " + month + "- " + year);
}

}

So on touching dateET which is an EditText it opens the datepicker, i set the new date and click set, it won't get dismissed, i click set again, even now it wont get dismissed, finally it gets dismissed when i click set for the 3rd time

Upvotes: 1

Views: 143

Answers (1)

Mohamed_AbdAllah
Mohamed_AbdAllah

Reputation: 5322

onTouch receives three events (normal sequence):

MotionEvent.ACTION_DOWN
MotionEvent.ACTION_MOVE
MotionEvent.ACTION_UP

and since you are not checking any of them (and returning true), your code is executed 3 times (one for each event).

Solution:

Use onClick() (this is what I would do) or add the check:

@Override
public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) { 
  case MotionEvent.ACTION_DOWN:

Calendar cal = Calendar.getInstance();
DatePickerDialog datePickDiag = new DatePickerDialog(
        QuickIWantActivity.this, QuickIWantActivity.this, cal
            .get(Calendar.YEAR), cal.get(Calendar.MONTH),
        cal.get(Calendar.DAY_OF_MONTH));
        datePickDiag.show();
        return false;
        }
    });
break;
}

Upvotes: 3

Related Questions