• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps on press and hold

I need a button to run a function while it is pressed down.
Then run another function when the button is released.

I am not seeing any method for this.

Help please?

I am trying:
btnDown.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(btnDown.isPressed()) {
displayDown.setBackgroundColor(Color.GREEN);
return false;
}

displayDown.setBackgroundColor(Color.RED);
return false;
}
});

I need to trigger an event when the button is released.
 
The MotionEvent class has Fields ACTION_UP, ACTION_DOWN, and ACTION_MOVE. You can use the getAction() method of the MotionEvent class, along with the aforementioned fields to get what you want.
 
The MotionEvent class has Fields ACTION_UP, ACTION_DOWN, and ACTION_MOVE. You can use the getAction() method of the MotionEvent class, along with the aforementioned fields to get what you want.

Thanks
This worked...

btnDown.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == android.view.MotionEvent.ACTION_DOWN ) {
displayDown.setBackgroundColor(Color.GREEN);
} else
if(event.getAction() == android.view.MotionEvent.ACTION_UP){
displayDown.setBackgroundColor(Color.RED);
}
return false;
}
});
 
Back
Top Bottom