Lucas
Lucas

Reputation: 191

Vibrate when hold down button

I am making an app where the user holds down a button to make the phone vibrate and i'm not sure how to make it so only when the button is being held down it vibrates, my code so far is.

package one.two.bn;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;



    public class Vb extends Activity {
            /** Called when the activity is first created. */
        private Button button1;
        private Vibrator vibrator;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            button1 = (Button)findViewById(R.id.button1);
            button1.setOnClickListener(new View.OnClickListener() {
            Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);    
        public void onClick(View v) {
                if(v==button1){ 
                vibrator.vibrate(300000);           
        }
    }

If any on can help thanks a lot.

Upvotes: 17

Views: 31370

Answers (5)

Yaroslav Mytkalyk
Yaroslav Mytkalyk

Reputation: 17115

If you need the long-press-feedback vibration just like ActionMode or ContextMenu do,

view.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
        return true;
    }
});

Upvotes: 47

FoamyGuy
FoamyGuy

Reputation: 46856

Use an OnTouchListener() instead of onClick().

Inside the listener when MotionEvent.getAction() == MotionEvent.ACTION_DOWN you will call vibrator.start(some arbitrarily large number here perhaps 1 minute or more)
And when getAction() == MotionEvent.ACTION_UP call vibrator.cancel().

That way it will start vibrating when you press down and stop when you lift up.

Upvotes: 7

Bipin Vayalu
Bipin Vayalu

Reputation: 3145

Try this code

Vibrator vibe = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE) ;

Then in the OnClickListener of your button:

vibe.vibrate(50); // 50 is time in ms

And dont forget you need to add the permission to the manifest (after the </application> tag):

<uses-permission android:name="android.permission.VIBRATE" />

I also agree with Tim, because onTouchListener is called before on click so it is give best output for your app.

Upvotes: 70

Lukas Knuth
Lukas Knuth

Reputation: 25755

button1.setOnClickListener(new View.OnClickListener() {
  [...]
public void onClick(View v) {
  if(v==button1){ // <- Don't need that...
    // Do Stuff...
  }
}});

If you use an inner Class for the 'onClickListener', you don't need to check if the View is the Button, because only the Button can trigger that 'onClick'-method.

Also, if you want the phone to vibrate as long as you hold down the Button, use the 'onLongClick'-method.

Upvotes: 1

Muhammad Imran Tariq
Muhammad Imran Tariq

Reputation: 23352

Add permissions for vibration in manifest file.

Upvotes: 0

Related Questions