Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Vibrate on touch?

I am trying to make my device vibrate when I touch an object on Screen. I am using this code:

 Vibrator v = (Vibrator) getSystemService(getApplicationContext().VIBRATOR_SERVICE); 
 v.vibrate(300);    

with the permission in the manifest file but I don't seem to get any results. Any suggestions? Also, my hardware supports vibrate.

like image 530
Fofole Avatar asked Jan 31 '12 12:01

Fofole


People also ask

Why does my Android vibrate when I touch it?

Go to your phone's settings. Go to “Sound and notification” or “Sound” depending on your phone. Some people will see the “Vibrate on touch” toggle here, but other's will have to go to “Other sounds” first. Toggle “Vibrate on touch” here.

How do I turn off vibrate on touch?

First, swipe down once from the top of the screen and tap the gear icon to open the Settings. Now go to the “Sounds and Vibration” section. Scroll down to “System Sound/Vibration Control.” The bottom section of toggles is for vibration.

How do I turn off vibrate on my Samsung touch?

Tap System sound/vibration control. This option may be called Vibrations. Tap the switch next to Samsung Keyboard. A gray switch means vibration has been disabled while a blue or green switch means it's still active.


3 Answers

According to this answer, you can perform haptic feedback (vibrate) without asking for any extra permissions. Look at performHapticFeedback method.

View view = findViewById(...)
view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

Note: I have not tested this code.

like image 53
Denilson Sá Maia Avatar answered Sep 28 '22 08:09

Denilson Sá Maia


please try this :

Button b = (Button) findViewById(R.id.button1);
    b.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Vibrator vb = (Vibrator)   getSystemService(Context.VIBRATOR_SERVICE);
            vb.vibrate(100);
            return false;
        }
    });

and add this permission to manifest.xml

 <uses-permission android:name="android.permission.VIBRATE"/>
like image 22
Ramesh Solanki Avatar answered Sep 28 '22 08:09

Ramesh Solanki


This will vibrate once, when user touches view (will not keep vibrating when user sliding his finger on the view!):

@Override
public boolean onTouch(View view, MotionEvent event) {

    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(VIBRATE_DURATION_MS);
    }
    return true;
}

And as Ramesh said, allow permission in manifest:

<uses-permission android:name="android.permission.VIBRATE"/>
like image 33
Mercury Avatar answered Sep 28 '22 08:09

Mercury