I have a dialog:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.location_dialog);
dialog.setTitle("My dialog");
dialog.setMessage("My dialog's content");
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.show();
I want to be able to detect touches over and outside the dialog box's lines. I can easily detect any touches outside the dialog box area with the build-in method
dialog.setCanceledOnTouchOutside(true);
But how can I detect the touches inside this area?
Create an extension of Dialog and override necessary method: dispatchTouchEvent or onTouchEvent (From docs: This is most useful to process touch events that happen outside of your window bounds, where there is no view to receive it.)
Updated:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Rect dialogBounds = new Rect();
getWindow().getDecorView().getHitRect(dialogBounds);
if (dialogBounds.contains((int) ev.getX(), (int) ev.getY())) {
Log.d("test", "inside");
} else {
Log.d("test", "outside");
}
return super.dispatchTouchEvent(ev);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With