Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving onTouch and onClick events with Android

I have a view that need to process onTouch gestures and onClick events. What is the proper way to achieve this?

I have an onTouchListener and an onClickListener set on the view. Whenever I do touch the view, first the onTouch event is triggered and later the onClick. However, from the onTouch event handler I have to return either true or false. Returning true means that the event is being consumed, so the android event system will not propagate the event any further.

Therefore, an onClick event is never generated, atleast my onClick listener is never triggered when I return true in my onTouch event handler. On the other hand, returning false there is not an option, since this prevents the onTouch listener from receiving any further events that are necessary in order to recognize a gesture. What's the usual way of solving this?

like image 752
theV0ID Avatar asked Jan 23 '13 13:01

theV0ID


People also ask

What is the difference between onTouch and Onclick Android?

onClickListener is used whenever a click event for any view is raised, say for example: click event for Button, ImageButton. onTouchListener is used whenever you want to implement Touch kind of functionality, say for example if you want to get co-ordinates of screen where you touch exactly.

How are touch events handled in Android?

You can react to touch events in your custom views and your activities. Android supports multiple pointers, e.g. fingers which are interacting with the screen. The base class for touch support is the MotionEvent class which is passed to Views via the onTouchEvent() method. you override the onTouchEvent() method.

What is onTouch Android?

onTouch(View v, MotionEvent event) Called when a touch event is dispatched to a view.

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.


2 Answers

In you GestureDetector, you can call callOnClick() directly. Note the View.callOnClick API requires API level 15. Just have a try.

 // Create a Gesturedetector
GestureDetector mGestureDetector = new GestureDetector(context, new MyGestureDetector());

// Add a OnTouchListener into view
m_myViewer.setOnTouchListener(new OnTouchListener()
{

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        return mGestureDetector.onTouchEvent(event);
    }
});

private class MyGestureDetector extends GestureDetector.SimpleOnGestureListener
{
    public boolean onSingleTapUp(MotionEvent e) {
        // ---Call it directly---
        callOnClick();
        return false;
    }

    public void onLongPress(MotionEvent e) {
    }

    public boolean onDoubleTap(MotionEvent e) {
        return false;
    }

    public boolean onDoubleTapEvent(MotionEvent e) {
        return false;
    }

    public boolean onSingleTapConfirmed(MotionEvent e) {
        return false;

    }

    public void onShowPress(MotionEvent e) {
        LogUtil.d(TAG, "onShowPress");
    }

    public boolean onDown(MotionEvent e) {            
        // Must return true to get matching events for this down event.
        return true;
    }

    public boolean onScroll(MotionEvent e1, MotionEvent e2, final float distanceX, float distanceY) {
        return super.onScroll(e1, e2, distanceX, distanceY);
    }        

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        // do something
        return super.onFling(e1, e2, velocityX, velocityY);
    }
}
like image 153
Autobots Avatar answered Sep 20 '22 06:09

Autobots


if you use onTouchListener , you don't have to use onClickListener. in onClickListener what it does is it get the touch event and check event actions and detect the click. so if you want to do some work when onClick. you can do it in the onTouchListener by filtering the action.

public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
    //this is touch

}
if (event.getAction() == MotionEvent.ACTION_UP) {
    //this is click

}
return false;
}
like image 38
Asanka Senavirathna Avatar answered Sep 18 '22 06:09

Asanka Senavirathna