Android: how to know if button is held down
android, button, callback, user-interface
Solution
You need to use an OnTouchListener to have separate actions for down, up, and other states.
mControls.UpButton.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Do something
return true;
case MotionEvent.ACTION_UP:
// No longer down
return true;
}
return false;
}
});
Problem
I am playing with a game on Android, and I have a function MoveCharacter (int direction) that moves an animated sprite when buttons are pressed For example, when user presses up I have this code: ``` mControls.UpButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mLevel.mCharAnimation.FrameUp(); } }); ``` However, I’d like to be able to keep moving the character as long as the user keeps the button down. Surprisingly, I have not found out how to do this in Android. Is there some kind of onButtonDownLister?