Check Touch out of relative layout

Check the x,y position on the screen you touch if it out of RelativeLayout or not..

Solution:

Step 1: In your activity, oerride this method: dispatchTouchEvent

Step 2: Find your layout via RelativeLayout yourlayoutID = (RelativeLayout) findViewById(R.id….bla bla bla);

Step 3: View your new method:


@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

float touchPointX = ev.getX();
float touchPointY = ev.getY();
int[] coordinates = new int[2];
yourLayout.getLocationOnScreen(coordinates);
if (touchPointX < coordinates[0] || touchPointX > coordinates[0] + yourLayout.getWidth() || touchPointY < coordinates[1] || touchPointY > coordinates[1] + yourLayout.getHeight()){
//Do Your ACTION
}
return super.dispatchTouchEvent(ev);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s