Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect if a touch event has landed within an EditText?

Tags:

android

I can get find getX(), and getY() both return a float. But how do I detect if the coordinates of a TouchEvent e.get(), e.getY() are within the boundaries of the EditText UI element? I notice getX() and getY() are floats but getHeight(), and getWidth() are int. Not going to help with comparisons ...

like image 747
Code Droid Avatar asked Jul 06 '12 23:07

Code Droid


1 Answers

What exactly do you want to do? If you only want to detect if your EditText is touched, add an OnTouchListener to the EditText... or even OnClickListener.

Edit: If you want to detect outside, you can detect touch event in the containing view, and then, given you have your EditText view:

Rect editTextRect = new Rect();
myEditText.getHitRect(editTextRect);

if (!editTextRect.contains((int)event.getX(), (int)event.getY())) {
    Log.d("test", "touch not inside myEditText");
}

Or you add a touch listener both to the EditText and the container, and return false in the one of the EditText, this way it will be intercepted and not forwarded to the parent. So, all the touches you detect in the listener of the parent, will not belong to the EditText.

like image 168
User Avatar answered Oct 12 '22 10:10

User