1

In one of my Activities, I have an ImageView which I set to it onTouchListener, in order to move it on the screen, the problem is that when I test it on debug mode everything works fine and I see that the ImageView is moving on the screen and I also can click on that. But, when I create a signed APK for release mode and test it on the same device, it doesn't respond!

the image view is inside a RelativeLayout and configured as:

 <ImageView
    android:id="@+id/imageView"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_centerInParent="true"
    android:src="@drawable/app_launcher"
    android:clickable="true"
 />

and the code for that is: (this code is found in onCreate method)

imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event))
            {
                Toast.makeTest(MainActivity.this, 
                                  "imageView clicked!",Toast.LENGTH_LONG).show();
                return true;
            } else {
                switch (event.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        initPosX= v.getX();
                        initPosY= v.getY();

                        initTouchX= event.getRawX();
                        initTouchY= event.getRawY();

                        lastAction= event.getAction();

                        return true;

                    case MotionEvent.ACTION_MOVE:
                        v.setX(initPosX+ (int)(event.getRawX() - initTouchX));
                        v.setY(initPosY+ (int)(event.getRawY() - initTouchY));

                        lastAction= event.getAction();
                        return true;

                    case MotionEvent.ACTION_UP:
                        v.setVisibility(View.VISIBLE);
                        lastAction= event.getAction();
                        return true;
                }
            }
            return true;

        }
    });

I have also a GestureDetectorListener to detect singleTap:

private class GestureListener extends GestureDetector.SimpleOnGestureListener{
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        return true;
    }
}
Elior
  • 3,178
  • 6
  • 37
  • 67
  • This does not seem to make sense. try with different devices or emulator . – Tixeon May 17 '18 at 09:34
  • That's why it's a weird behavior! I tried it with different devices. on debug everything is working, on release not – Elior May 17 '18 at 10:20

1 Answers1

0

Declare gesture class into on create:

final GestureDetector mGesDetect = new GestureDetector(this, new GestureListener());

After that put image Touch Listner like below code:

 imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mGesDetect.onTouchEvent(event);
            return true;
        }
    });


   private class GestureListener extends GestureDetector.SimpleOnGestureListener{
       @Override
        public boolean onSingleTapUp(MotionEvent e) {
           return true;
        }

       @Override
       public boolean onSingleTapConfirmed(MotionEvent e) {
         return true;
       } 
   }
Mahesh Keshvala
  • 1,349
  • 12
  • 19