Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:38:00+00:00 2026-06-05T17:38:00+00:00

I have a simple slide action for LinearLayout View using TouchListener . The problem

  • 0

I have a simple slide action for LinearLayout View using TouchListener.

The problem is MotionEvent.ACTION_MOVE keeps being called when I touch and move.

How can I stop MotionEvent.ACTION_MOVEfrom being called?

v.setOnTouchListener(null) is not good: returns false and is not working.

I want MotionEvent.ACTION_MOVE to be called just one time.

    private View.OnTouchListener touchEvent = new View.OnTouchListener() {
        private float tempX;
        private final float BASE_VALUE = 100;
        private final boolean LEFT = false;
        private final boolean RIGHT = true;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            if(event.getAction() == MotionEvent.ACTION_DOWN){
                tempX = event.getX();
            }

            if(event.getAction() == MotionEvent.ACTION_MOVE){
                if(event.getX() < (tempX + BASE_VALUE)){
                    changeView(LEFT);
                }else if(event.getX() > (tempX + BASE_VALUE)){
                    changeView(RIGHT);
                }
            }

It is not working perfectly.

But this implementation is not bad (this code is my idea).

The code:

private View.OnTouchListener touchEvent = new View.OnTouchListener() {
    private VelocityTracker mVelocityTracker;
    private float tempX;
    private final int MIN_DISTANCE = 120;
    private final int THRESHOLD_VELOCITY = 200; 
    private final boolean LEFT = false;
    private final boolean RIGHT = true;
    private MotionEvent cancelEvent = MotionEvent.obtain(100, 100, MotionEvent.ACTION_CANCEL, 0, 0, 0);
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            Log.d(TAG, "Down");
            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(event);
            tempX = event.getX();
        }
        if(event.getAction() == MotionEvent.ACTION_MOVE){
            mVelocityTracker.addMovement(event);
            mVelocityTracker.computeCurrentVelocity(100);
            float velocityX = mVelocityTracker.getXVelocity();
            if(tempX - event.getX() > MIN_DISTANCE 
                   && Math.abs(velocityX) > THRESHOLD_VELOCITY){
                   Log.d(TAG, "LEFT");
                   changeView(LEFT);
            }else if(event.getX() - tempX > MIN_DISTANCE 
                   && Math.abs(velocityX) > THRESHOLD_VELOCITY){
                   Log.d(TAG, "RIGHT");
                   changeView(RIGHT);
            }
            v.dispatchTouchEvent(cancelEvent);
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:38:02+00:00Added an answer on June 5, 2026 at 5:38 pm

    Make SWIPELISTENER.

    The Code.

    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    
    
    public abstract class OnSwipeListener implements OnTouchListener {
    public final static int SWIPED_LEFT = 1;
    public final static int SWIPED_RIGHT = 2;
    public final static int SWIPED_UP = 3;
    public final static int SWIPED_DOWN = 4;
    public final static int CLICKED = 5;
    
    private int SWIPE_ACTION = 0;
    private float firstTouchX = 0;
    private float firstTouchY = 0;
    private float lastTouchX = 0;
    private float lastTouchY = 0;
    private int activePointerID;
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        if(event.getAction() == event.ACTION_DOWN){
            final float x = event.getX();
            final float y = event.getY();
    
            firstTouchX = x;
            firstTouchY = y;
    
            activePointerID = event.getPointerId(0);
    
            v.setBackgroundResource(R.drawable.layoutbutton2);
        }
    
        if(event.getAction() == event.ACTION_UP){
            float dX = lastTouchX - firstTouchX;
            float dY = lastTouchY - firstTouchY;
    
            final int pointerIndex = event.findPointerIndex(activePointerID);
    
            if((firstTouchX + 10) == event.getX(activePointerID) &&
                    (firstTouchY + 10) == event.getY(activePointerID)){
    
                SWIPE_ACTION = CLICKED;
    
            }else{
                if(Math.abs(dX) > Math.abs(dY)){
                    if(dX > 0){
                        SWIPE_ACTION = SWIPED_RIGHT;
                    }else{
                        SWIPE_ACTION = SWIPED_LEFT;
                    }
                }else{
                    if(dY > 0){
                        SWIPE_ACTION = SWIPED_DOWN;
                    }else{
                        SWIPE_ACTION = SWIPED_UP;
                    }
                }
            }
            v.setBackgroundResource(R.drawable.layoutbutton1);
            onSwipe(v, event);
        }
        if(event.getAction() == event.ACTION_MOVE){
            final int pointerIndex = event.findPointerIndex(activePointerID);
            final float x = event.getX(pointerIndex);
            final float y = event.getY(pointerIndex);
    
            lastTouchX = x;
            lastTouchY = y;
    
            v.setBackgroundResource(R.drawable.layoutbutton1);
        }
        if(event.getAction() == event.ACTION_CANCEL){
            v.setBackgroundResource(R.drawable.layoutbutton1);
        }
    
        return true;
    
    }
    
    public int getSwipeTo(){
        return SWIPE_ACTION;
    }
    
    abstract public void onSwipe(View v, MotionEvent event);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have this simple code $('#info_name'+index).show(slide, {direction: right}, 1000, function(){ $('#info_info'+index).show('slide',{direction: up}, 1000);
I have, what I estimate, to be a simple problem. I have a side
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
I have a simple slideshow which automatticlly changes the image using setInterval every 5
In my form i have a simple <select multiple=multiple name=action[files][] id=action_files></select> outputed by :
I have a simple web-site. Almost every action takes int toonId as an argument
I have some content sliding here. http://www.smallsharptools.com/downloads/jQuery/Slider/slider.html The HTML structure is simple. There is
I have simple win service, that executes few tasks periodically. How should I pass
i have simple regular expression: ^123$ Matches are for example 123 1234 etc. How
I have simple issue setting a two-way databinding of a checkbox in Silverlight 3.0.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.