Vancore
Vancore

Reputation: 697

Android: Disable Buttons on Screen-Tap

I want to disable my shown buttons when a user performs a single-tap on the screen. I currently have a ViewPager on my MainActivity and the mentioned buttons.

Which Listener do I have to use in order to fetch the single-tap event and where do I have to "put" it on, so it does not override my swipe-gesture from my viewPager? (I tried to put an onTouchListener on my viewPager which did work, but I could not swipe anymore, obviously)

My code for illustration (please be gentle, I am totally new to Android :) ):

public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener{ 
(some Code, onCreate etc...)
@Override
public boolean onTouchEvent(MotionEvent event) {
        int action = MotionEventCompat.getActionMasked(event);

        switch(action) {
            case (MotionEvent.ACTION_DOWN) :
                Log.d(DEBUG_TAG, "Action was DOWN");
                return true;
            case (MotionEvent.ACTION_MOVE) :
                Log.d(DEBUG_TAG,"Action was MOVE");
                return true;
            case (MotionEvent.ACTION_UP) :
                triggerButtons();
                Log.d(DEBUG_TAG,"Action was UP");
                return true;
            case (MotionEvent.ACTION_CANCEL) :
                Log.d(DEBUG_TAG,"Action was CANCEL");
                return true;
            case (MotionEvent.ACTION_OUTSIDE) :
                Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
                        "of current screen element");
                return true;
            default :
                return super.onTouchEvent(event);
        }
}

@Override
public boolean onDown(MotionEvent e) {
    return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    triggerButtons();
    return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    return false;
}

So as you can see, I want my "triggerButtons" to be fired, when the user lifts his finger after a touch/tap happened. What listener do I need to assign in my onCreate to make this work?

(if you also know how I can listen to a doubleTap, that would be so great!)

Edit:

I have added

View.OnTouchListener mOnTouchListener; 
GestureDetector mGestureDetector; 

This I added to my onCreate in my MainActivity:

mOnTouchListener = new View.OnTouchListener() { 
    @Override public boolean onTouch(View v, MotionEvent event) {  
    mGestureDetector.onTouchEvent(event); 
    return false; 
   } 
}; 

and

mViewPager.setOnTouchListener(mOnTouchListener); 

Solution:

In onCreate:

mOnTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            touchEventDecider(event);
            return false;
        }
    };

Method touchEventDecider:

private boolean touchEventDecider(MotionEvent event) {
    int action = MotionEventCompat.getActionMasked(event);

    switch(action) {
        case (MotionEvent.ACTION_DOWN) :
            Log.d(DEBUG_TAG, "Action was DOWN");
            return true;
        case (MotionEvent.ACTION_MOVE) :
            Log.d(DEBUG_TAG,"Action was MOVE");
            return true;
        case (MotionEvent.ACTION_UP) :
            triggerButtons();
            Log.d(DEBUG_TAG,"Action was UP");
            return true;
        case (MotionEvent.ACTION_CANCEL) :
            Log.d(DEBUG_TAG,"Action was CANCEL");
            return true;
        case (MotionEvent.ACTION_OUTSIDE) :
            Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
                    "of current screen element");
            return true;
        default :
            return super.onTouchEvent(event);
    }
}

Upvotes: 2

Views: 229

Answers (1)

Lingviston
Lingviston

Reputation: 5671

Add OnTouchListener to your ViewPager. In onTouchEvent method of your TouchListener pass each event to your GestureDetector and return false. In GestureDetector return true in onDown and leave only onSingleTapUp as you have it.

Your problem is that you consume every touch event in your onTouchEvent method by returning true thus events are not coming to your ViewPager making it unscrollable.

EDIT: If you want to recognize single tap by yourself without using GestureDetector then just attach OnTouchListener to your ViewPager as you did but don't forget to return false there in onTouchEvent method.

Upvotes: 2

Related Questions