Is there a way to get an onButtonDown or onButtonUp event for a soft button (i.e. not a physical hardware button, but a button on the screen)? I have a button on the screen that I want the user to hold down for X seconds. To do this I need to capture the buttonDown and buttonUp events separately.
Thanks,
Bret
In Android applications, a Button is a user interface that is used to perform some action when clicked or tapped. It is a very common widget in Android and developers often use it.
In android, we have a different type of buttons available to use based on our requirements, those are ImageButton, ToggleButton, RadioButton. In android, we can create a Button control in two ways either in the XML layout file or create it in the Activity file programmatically.
yourButton.setOnTouchListener( yourListener );
public boolean onTouch( View yourButton , MotionEvent theMotion ) {
switch ( theMotion.getAction() ) {
case MotionEvent.ACTION_DOWN: break;
case MotionEvent.ACTION_UP: break;
}
return true;
}
Kotlin variant
button.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
when (event?.action) {
MotionEvent.ACTION_DOWN -> { }
MotionEvent.ACTION_UP -> { }
}
return v?.onTouchEvent(event) ?: true
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With