Triggering Event When Button Is Pressed Down in Android

Triggering event when Button is pressed down in Android

Maybe using a OnTouchListener? I guess MotionEvent will have some methods for registering a touch on the object.

   button.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}))

Triggering multiple buttons(onClick event) with one swipe/gesture

I was looking for a solution, and finally made it work. So I will answer here and hope it will help to someone.

I had a problem detecting a swipe with a layout with multiple buttons, so I override the method setOnTouchListener for every button, calling inside gesturedetector.onTouchEvent(event) (and do what you want to do with the event detected).

Obviously, to detect the button click event you only have to use setOnClickListener method.

Maybe it's no the most elegant way to make this work, but at least worked for me.

Capture button release in Android

You should set an OnTouchListener on your button.

button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
increaseSize();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
resetSize();
}
}
};

listener for pressing and releasing a button

You can use a onTouchListener:

view.setOnTouchListener(new View.OnTouchListener() {        
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
// RELEASED
return true; // if you want to handle the touch event
}
return false;
}
});

Sliding finger across screen activating buttons as if they were pressed

I actually took the "easy" way out and used buttons with an onTouch-method using ACTION_DOWN and ACTION_MOVE with coordinate calculations that combined with event.getX() and event.getY() allows me to detect which button is currently hovered. It's lag free with 13 buttons.



Related Topics



Leave a reply



Submit