Search code examples
androidandroid-vibration

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.


Solution

  • 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"/>