Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get a view from an event coordinates in android?

I want to intercept the touch events on my parent view with onInterceptTouchEvent (MotionEvent ev).

From there I want to know which view was clicked in order to do other things, is there any way to know which view was clicked from that motion event received?

like image 770
htafoya Avatar asked Nov 12 '10 14:11

htafoya


People also ask

How to handle touch event in Android?

Setup a touch listener In order to make your OpenGL ES application respond to touch events, you must implement the onTouchEvent() method in your GLSurfaceView class. The example implementation below shows how to listen for MotionEvent. ACTION_MOVE events and translate them to an angle of rotation for a shape.

What is touch slop?

"Touch slop" refers to the distance in pixels a user's touch can wander before the gesture is interpreted as scrolling.


3 Answers

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.

like image 177
htafoya Avatar answered Nov 01 '22 09:11

htafoya


A simple way for getting the touched view is to set an OnTouchListener to the individual views and store the view in a class variable of the activity. Returning false will make the input event available to the method onTouchEvent() of the activity, where you can easily handle all the touch events (also the ones of your parent view).

myView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
    touchedView = myView;
    return false;
    }
}); 


@Override
public boolean onTouchEvent(MotionEvent event) {


    switch (event.getAction()) {

        case MotionEvent.ACTION_UP:

            if(touchedView!=null) {
                doStuffWithMyView(touchedView);
            ....
            ....
like image 30
Christopher Masser Avatar answered Nov 01 '22 11:11

Christopher Masser


Just to make method of htafoya simpler:

/**
* 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
*/
private 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
    return ((x > viewX && x < (viewX + view.getWidth())) &&
            (y > viewY && y < (viewY + view.getHeight())));
}
like image 27
schmidt9 Avatar answered Nov 01 '22 11:11

schmidt9