Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long Press Button Event Handler

Tags:

android

I have seen several articles such as this one describing how to handle a long press event with a button. I can follow these directions but I am wondering if it is possible to do it the same way I handled a click. The way I handled a click was to define the handler in XML as such:

<Button
    android:id="@+id/btn_NextLift"
    ...
    android:onClick="btn_NextLiftClick" />

then in code as such:

public void btn_NextLiftClick(View vw_Current) 
    {...}

I do see the boolean property longClickable in the xml but I don't see where to define an event handler so...???

TIA JB

like image 814
GPGVM Avatar asked Nov 14 '12 16:11

GPGVM


1 Answers

You can't do this via XML. Instead, use:

Button button = (Button) findViewById(R.id.btn_NextLift);

button.setOnLongClickListener(new OnLongClickListener() { 
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            return true;
        }
    });

Make sure this code comes after setContentView() has been called.

Also, make sure that the longClickable property is set to true.

like image 139
Raghav Sood Avatar answered Oct 17 '22 04:10

Raghav Sood