how to get a view from an event coordinates in android?
Well for anyone who wants to know what I did ... i couldn't. I did a workaround to just know if my specific view component was clicked, so I could only end with this:
if(isPointInsideView(ev.getRawX(), ev.getRawY(), myViewComponent)){
doSomething()
}
and the method:
/**
* Determines if given points are inside view
* @param x - x coordinate of point
* @param y - y coordinate of point
* @param view - view object to compare
* @return true if the points are within view bounds, false otherwise
*/
public static boolean isPointInsideView(float x, float y, View view){
int location[] = new int[2];
view.getLocationOnScreen(location);
int viewX = location[0];
int viewY = location[1];
//point is inside view bounds
if(( x > viewX && x < (viewX + view.getWidth())) &&
( y > viewY && y < (viewY + view.getHeight()))){
return true;
} else {
return false;
}
}
However this only works for known views in the layout that you can pass as parameter, I still can't get the clicked view just by knowing the coordinates. You may search for all views in the layout though.
How to get the absolut position of a touch event?
You can use event.getRawX()
and event.getRawY()
to get position relative to screen.
how to Get coordinates from Custom view android
Use interface or callback after finishing your touchEvent. you have to re-call your
findCoordinates()
so it will update your UI respect to new values.
How can I get the x/y location of a touch relative to the entire view when I touch a button?
To get the touch position relative to the view add the touch event's (x,y) to your button's (x,y); to get the button's position relative to it's parent (your view) use the getLeft()
and getTop()
methods.
The documentation for View
has the following under Position:
It is possible to retrieve the location of a view by invoking the
methods getLeft() and getTop(). The former returns the left, or X,
coordinate of the rectangle representing the view. The latter returns
the top, or Y, coordinate of the rectangle representing the view.
These methods both return the location of the view relative to its
parent. For instance, when getLeft() returns 20, that means the view
is located 20 pixels to the right of the left edge of its direct
parent.
Example code for your onTouch
method:
float screenX = v.getLeft() + event.getX(); // X in Screen Coordinates
float screenY = v.getTop() + event.getY(); // Y in Screen Coordinates
Get the real X/Y position of an onTouch Event
Try using getRawX() and getRawY() instead of getX and getY.
Edit
I think that i have found the problem.
You are obtaining X and Y relative to the GridView top-left corner, not to the absolute screen coordinetes.
What you can do is the following:
int[] offset = new int[2];
center.getLocationOnScreen(offset);
int Xoffset=offset[0];
int Yoffset = offset[1];
private void animation(int posX, int posY, int etat){
//...
img.setX(posX+Xoffset);
img.setY(posY+Yoffset);
[...]
}
This is supposed to set the the top-left corner of the ImageView in the selected point. In order to set the center in that point:
int ivWidth = img.getWidth();
int ivHeight = img.getHeight();
private void animation(int posX, int posY, int etat){
//...
int[] finalPosition=new int[2];
finalPosition[0] = posX+Xoffset-(ivWidth/2);
finalPosition[1] = posY+Yoffset-(ivHeight/2);
img.setX(finalPosition[0]);
img.setY(finalPosition[1]);
[...]
}
I haven't try it but it should work.
Edit 2
Xoffset and Yoffset are only needed if you use getX()/getY() instead of getRawX()/getRawY()
Get real x and y coordinates of users touch
I use this code to get the actually axis of touch on screen:
PointF p = new PointF(event.getX(), event.getY());
View v = this;// your view
View root = v.getRootView();
while (v.getParent() instanceof View && v.getParent() != root) {
p.y += v.getTop();
p.x += v.getLeft();
v = (View) v.getParent();
}
Hope this works for you as well
How to get the Touch position in android?
@Override
public boolean onTouchEvent(MotionEvent event)
{
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
return false;
}
The three cases are so that you can react to different types of events, in this example tapping or dragging or lifting the finger again.
How to get the absolute coordinates of a view
Use View.getLocationOnScreen()
and/or getLocationInWindow()
.
Related Topics
How to Keep a Button as Pressed After Clicking on It
Get The Displayed Size of an Image Inside an Imageview
Android: How to Download a Video File to Sd Card
How to Get Total Ram Size of a Device
How to Get My Android Device Internal Download Folder Path
How to Find Android Textview Number of Characters Per Line
How to Scale a Streaming Bitmap In-Place Without Reading The Whole Image First
Android Intercept Recent Apps Button
Using Espresso to Unit Test Google Maps
Reasons That The Passed Intent Would Be Null in Onstartcommand
Onoptionsitemselected Not Called When Using Actionlayout (Sherlockactionbar)
How to Use a Library Project in Android Studio
Is There Another Place to Get a Google-Services.JSON
Android - How to Get View from Context
How to Get Thumbnail for Video in My /Sdcard/Android/Data/Mypackage/Files Folder
How to Store Files Generated from App in "Downloads" Folder of Android