0

i want to repeat some instruction example counter while pressing and holding in an image drew in my SurfaceView, i tried android-repeat-action-on-pressing-and-holding-a-button but when i touch the image the counter stay counting even after releasing.

here is my code :

public class Graphics extends Activity implements OnTouchListener{
.
.
.
private Handler mHandler;
private Runnable mAction = new Runnable() {
    @Override public void run() {
        myCounter++;
        mHandler.postDelayed(this, 500);
    }
};
.
.
@Override
public boolean onTouch(View v, MotionEvent event) {
    x = event.getX();
    y = event.getY();
    if(/*testing if x and y in the right positions*/){
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mHandler != null) return true;
                mHandler = new Handler();
                mHandler.postDelayed(mAction, 500);
                break;
            case MotionEvent.ACTION_UP:
                if (mHandler == null) return true;
                mHandler.removeCallbacks(mAction);
                mHandler = null;
                break;
            }
            return false;
    }
return false;
}
Community
  • 1
  • 1
user3578318
  • 65
  • 1
  • 6

0 Answers0