Ted pottel
Ted pottel

Reputation: 6983

Trying to detect if a button was held down for about 3 seconds

I saw an app where if you press the button it would play a ring tone, then if you kept it down, a message boxed came up asking you if you wish to save the sound file. I would like to have my app detect if a button was pressed down for like a couple seconds.

I cannot figure out how to detect if a button was held down for a long time. I tried Googling it, but came up empty.

Upvotes: 0

Views: 1560

Answers (3)

daniel_c05
daniel_c05

Reputation: 11528

Apparently you have not read about onLongClickListener huh?

Read the following links:

http://developer.android.com/reference/android/view/View.OnLongClickListener.html

example code:

buttonPlay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Do something when clicked normally.
        }
    });

    buttonPlay.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            //Offer additional options when the view is held. 
            return true;
        }
    });

Also, you may call registerForContextMenu() instead and pass the View you want to set the listener to. Reference

Sample code:

registerForContextMenu(textView);//Registering the TextView textview for longclicks

You must write the code to inflate the context menu:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.your_context_menu, menu);
    }       
}   

Where the id your_context_menu must be an .xml file created under the Res/Menu/ folder on your project.

Once the menu is inflated, you must also write the code to respond to options being selected.

@Override
public boolean onContextItemSelected(MenuItem item) {
    Intent broadcast = new Intent();
    switch (item.getItemId()) {
    case R.id.menu_option_one:          
    //do something
    return true;
    default:
        return super.onContextItemSelected(item);
    }
}

Upvotes: 1

devunwired
devunwired

Reputation: 63303

It depends on how specific you are on the "like a couple seconds" part. You can set an OnLongClickListener on any view (including Button...docs link), which is a standard system event that users will understand. The timeout for this event, however, is 500ms.

If you truly need to have the finger down for multiple seconds, you will need to time this event yourself, in which case an OnTouchListener that starts a Handler on ACTION_DOWN and checks the view state after postDelayed() typically does the trick.

Upvotes: 1

anthropomo
anthropomo

Reputation: 4120

You are looking for OnGestureListener and specifically onLongPress:

http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html#onLongPress(android.view.MotionEvent)

And if you only want to use onLongPress, you'll want to use the convenience class:

http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html

Upvotes: 1

Related Questions