jaredad7
jaredad7

Reputation: 1060

How can I make a piece of code loop while a Button is pressed and stop it once the Button is no longer Pressed?

I am using the android developer tools in Eclipse, programming in Java, and I need to make an object move across the screen as long as a button is pressed. I've been doing research for hours, and I cannot find any methods to accomplish this. I've tried running threads, which often crash or seemingly don't execute. I've also tried an onClickListener which reads the button state and uses it to determine whether or not the button is still pressed. I'm currently using a while loop, but this just freezes the program. I believe that this is the best method, and I've tried to use Thread.sleep in order to limit the number of iterations per second, as I believe that this is the reason it is freezing. Am I on the right track or am I way off in left field? Here is a snippet of code:

rightButton.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0) 
        {
            while(arg0.isPressed())
            {
                mover.updateCoordinates(1,  0);
            }
        }

    }); 

Upvotes: 1

Views: 1790

Answers (3)

Tushar
Tushar

Reputation: 8049

Try:

myView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent e) {
        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            // Start your animation here
        } else if (e.getAction() == MotionEvent.ACTION_UP) {
            // Stop your animation here
        }

       return false;
    }
});

References:

MotionEvent

OnTouchListener

Alternatively, override onKeyUp and onKeyDown of the View class and check for the KEYCODE_ENTER KeyEvent

Upvotes: 0

Chor Wai Chun
Chor Wai Chun

Reputation: 3236

Would you try this another method?

Firstly declare your button as class variable, declare a Handler and a Runnable:

private Button rightButton; // You will assign this in onCreate() method
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
    @Override
    public void run() {
        if(rightButton.isPressed())
        {
            // If press state is pressed, move your item and recall the runnable in 100 milliseconds.
            mover.updateCoordinates(1,  0);
            mHandler.postDelayed(mRunnable, 100);
        }
    }
};

Then your button's onClickListener will looks like this:

rightButton.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View arg0) 
    {
        // Instead of performing a loop here, just call a runnable, do simple press state checking there.
        mHandler.postDelayed(mRunnable, 100);
    }

}); 

Upvotes: 2

isaach1000
isaach1000

Reputation: 1839

Create a loop that updates the views, waits, and calls itself after it finishes waiting. Within the loop, have an animation if statement with a boolean field that move on true and does not move on false. Have the onClick method toggle the boolean field's value.

Upvotes: 0

Related Questions