0

I want the device to vibrate as long as the user clicks the button. If the user press the button for a long period of time the device is to vibrate as long as the user clicks and holds. here is what I implemented but it works for a particular period of time.

final Button button = (Button) findViewById(R.id.button1);

button.setOnLongClickListener(new OnLongClickListener() {   

    @Override
    public boolean onLongClick(View v) {
        if(v==button){
            Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vb.vibrate(1000);
       }                
       return false;
    }
});
jenzz
  • 7,239
  • 6
  • 49
  • 69
user2491565
  • 3
  • 1
  • 3

3 Answers3

1

implement ontouch listener and perform this action in event down

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

    //int action = event.getAction();
    switch (event.getAction()){
    case MotionEvent.ACTION_DOWN:
       // put your code here


    break;
anddevmanu
  • 1,459
  • 14
  • 19
  • Thanks anddevvmanu : but how do i perform vibration if user clicks and holds button for long period of time.the device is to vibrate continuously in this scenario. – user2491565 Jun 16 '13 at 21:45
  • have you tried with this code with this event this gives action until you up your finger. so if you press for long time you receive the event of finger pressed – anddevmanu Jun 16 '13 at 21:49
1

To vibrate indefinitelly you can use vibration pattern:

// 0 - start immediatelly (ms to wait before vibration)
// 1000 - vibrate for 1s
// 0 - not vibrating for 0ms
long[] pattern = {0, 1000, 0};
vb.vibrate(pattern, 0);

Vibration can be cancelled using:

vb.cancel();

Start vibration on onTouch event - ACTION_DOWN, stop vibration on ACTION_UP

EDIT: final working code:

    final Button button = (Button) findViewById(R.id.button1);
    button.setOnTouchListener(new OnTouchListener() {
        Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    long[] pattern = {0, 1000, 0};
                    vb.vibrate(pattern, 0);
                    break;
                case MotionEvent.ACTION_UP:
                    vb.cancel();
                    break;
            }
            return false;
        }
    });
lopisan
  • 7,720
  • 3
  • 37
  • 45
  • can you plese suggest what is wrong in my code here final Button button = (Button) findViewById(R.id.button1); button.setOnTouchListener(new OnTouchListener() { Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()){ case MotionEvent.ACTION_DOWN: // put your code here long[] pattern = {0, 1000, 0}; vb.vibrate(pattern, 0); case MotionEvent.ACTION_UP: vb.cancel(); break; } return false; } }); } – user2491565 Jun 16 '13 at 22:45
  • I've noticed missing break in case ACTION_DOWN (so the vibration is cancelled immediately) – lopisan Jun 16 '13 at 22:47
0

In Kotlin:

You can call the performHapticFeedback method on the view reference you obtained by binding

binding.root.performHapticFeedback(HapticFeedbackConstants.CONFIRM)

Not required vibration permission, you can see other cosntants HapticFeedbackConstants

Codelaby
  • 2,604
  • 1
  • 25
  • 25