0

I am trying to get an object to respond to a click on hold. I can get it to work by pressing the screen hundreds of times. I put log lines to test it. I only get one log entry per click, but I want repeating entries until I let go. There is a question here that has the same request but the only answer does not fully cover the topic and does not address all of the problems with that solution. There must be a simpler way. Angry Birds uses a click and hold. I really think a code sample is needed but here one is if you want it.

public class GameView extends SurfaceView implements Runnable {

    private Thread thread;
    private boolean isPlaying;

    public GameView( GameActivity activity, int screenx, int screeny ) {
        super( activity );

    //tried it here
        /*setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d( "note", "hold" );
                if(event.getAction() == MotionEvent.ACTION_UP){

                    // Do what you want
                    return true;
                }
                return false;
            }
        });*/
    }

    @Override
    public void run() {
        while ( isPlaying ) {
            //do stuff
        draw();
        }
    }

    public void draw () {
        if ( getHolder().getSurface().isValid() ) {
            Canvas canvas = getHolder().lockCanvas();
        //tried it here
            setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d( "note", "hold" );
                if(event.getAction() == MotionEvent.ACTION_UP){
                    // Do what you want
                    return true;
                }
                return false;
            }
        });
            getHolder().unlockCanvasAndPost( canvas );
        }
    }

    public void resume () {
    isPlaying = true;
        thread = new Thread( this );
        thread.start();
    }

    public void pause () {
    try {
        isPlaying = false;
                thread.join();
    } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //tried it here
    Log.d( "note", "press" );
        return true;
    }
}

Almost there, here is what I have so far. The only problem remaining is if I click and hold on the screen and then move to another position I do not get another mouseEvent. I need the event.getY().

public boolean mouseDown = false;

public boolean onTouchEvent(MotionEvent event) {
    Log.d( "note", "press" );
    switch ( event.getAction() ) {
        case MotionEvent.ACTION_DOWN:
        Log.d( "note", "down" );
        mouseDown = true;
        mouseLooper(  );
        break;
        case MotionEvent.ACTION_UP:
        Log.d( "note", "up" );
        mouseDown = false;
        break;
    }
    return true;
}

public void mouseLooper ( ) {
        while ( mouseDown ) {
            Log.d( "note", "looping" );
        }
}

***Can I read the mouse position outside of a TouchEvent?

Got it, the log entry "press" does appear when the mouse or finger is moved, as many times as the mouse or finger is moved. From mouseLooper you can access the current y position, or what ever else you send to it. Here is the working code sample.

public boolean mouseDown = false;

public boolean onTouchEvent(MotionEvent event) {
    Log.d( "note", "press" );
    mouseLooper( (int) event.getY() );
    switch ( event.getAction() ) {
        case MotionEvent.ACTION_DOWN:
        Log.d( "note", "down" );
        mouseDown = true;
        mouseLooper( (int) event.getY() );
        break;
        case MotionEvent.ACTION_UP:
        Log.d( "note", "up" );
        mouseDown = false;
        break;
    }
    return true;
}

public void mouseLooper ( int currentY ) {
        while ( mouseDown ) {
            Log.d( "note", "looping" );
        }
}
Goff
  • 343
  • 1
  • 3
  • 14

1 Answers1

1

you can use this code :

    yourBtn.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //code that you want do when pressed
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            //code when touch stoped
        }
        return false;
    }
});

you must remove onTouch override at the end of code.

if you want repeat a code, you can use while or a thread with handler.

alireza daryani
  • 787
  • 5
  • 16