Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ButtonDown and ButtonUp events for Android screen buttons?

Tags:

android

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

like image 979
Bret Avatar asked May 01 '10 17:05

Bret


People also ask

What are Android buttons?

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.

How many types of buttons are there in Android?

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.


2 Answers

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;
}
like image 157
drawnonward Avatar answered Sep 23 '22 18:09

drawnonward


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
        }
    })
like image 41
Dan Alboteanu Avatar answered Sep 19 '22 18:09

Dan Alboteanu