Search code examples
androidhandlerrunnable

OnTouchListener Delay not functioning


I cannot for the life of me find any help online to describe to me why the delay for the "ACTION_DOWN" is not functional. The rest of my code executes as soon as I touch the image. My intent is placed between the //additional code and //end additional code comments. Does it need to be placed elsewhere? Thanks in advance!

Edit: I would use an onLongClickListener but my intention is to extend the time the object has to be held in order for the intent to begin.

mImage = (ImageView)MainActivity.this.findViewById(R.id.Floaterimg);
    mImage.setOnTouchListener(new View.OnTouchListener() {

        final Handler handler2 = new Handler(); 
        Runnable mLongPressed = new Runnable() { 
            public void run() { 
                Log.i("", "Long press!");

            }
        };
            @Override
            public boolean onTouch(View v, MotionEvent event){
                if(event.getAction() == MotionEvent.ACTION_DOWN)
                    handler2.postDelayed(mLongPressed, 3000);

                                    //additional code
                                    //end of additional code

                if((event.getAction() == MotionEvent.ACTION_MOVE)||
(event.getAction() == MotionEvent.ACTION_UP))
                    handler2.removeCallbacks(mLongPressed);
                    return false;    
            }
        });

Solution

  • Why dont you use mImage.setOnLongClickListener()?