26

Is there a way to get a button to vibrate but only when the if condition is verified?

Here's the code:

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

if(l2>=l1){
        insertactone.setBackgroundColor(Color.RED);

    };

here is the onclick method for insertactone:

einsertactone = (Button) findViewById(R.id.bsqlinsertactone);
    insertactone.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.bsqlinsertactone:
                insertactoneClick();
                break;
            }
        }

        private void insertactoneClick() {
            startActivity(new Intent(
                    "com.example.everydaybudgetplanner.ACTONESQLENTRY"));
        }

    });

I want it to vibrate only if the the IF condition is verified.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Rui Miranda
  • 319
  • 1
  • 4
  • 9

5 Answers5

58

is there a way to get a button to vibrate but only when the if condition is verified?

Yes. It looks like you have 95% of the code there already. Where did you get stuck?

You already have a Vibrator Object and a conditional. All you need to do now is call vibrate() like so:

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

if(l2 >= l1) {
    insertactone.setBackgroundColor(Color.RED);
    vibe.vibrate(100);
}

Don't forget that you need to add

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

in your AndroidManifest.xml file.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • it still doesn't work the way i want it to! i just want it to vibrate when the user clicks it if its already RED – Rui Miranda Nov 22 '13 at 16:27
  • 1
    What have you done to debug it? What does "it doesn't work" mean? Does it crash? Does it compile? – Bryan Herbst Nov 22 '13 at 16:28
  • nothing like that! the problem is, it vibrates everytime i start the activity that contains thiss code. i want it just to vibrate when i press the button – Rui Miranda Nov 22 '13 at 16:34
  • i want it to vibrate just on click – Rui Miranda Nov 22 '13 at 16:35
  • Then move the `vibrate()` call (and the conditional) to `onClick()`. – Bryan Herbst Nov 22 '13 at 16:37
  • for that i have to change l2 and l1 to final and i can't because they get their value from a select SUM – Rui Miranda Nov 22 '13 at 16:58
  • 1
    One option would be to change your Activity to implement OnClickListener (instead of creating an anonymous OnClickListener as you do now). – Bryan Herbst Nov 22 '13 at 17:04
  • I used this solution and it worked well on most brand phones. But then I received some error reports on many Samsung's phone that got null from [(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);.] I wonder what's wrong there? This is the crash when doing vibe.vibrate(100); [Attempt to read from field 'android.os.VibrationEffect com.android.server.VibratorService$Vibration.mEffect' on a null object reference] – Fisher May 06 '21 at 13:10
3

For Kotlin:

First of all, you have to add this to your Manifest

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

Code:

private var vibration = activity?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator

if (Build.VERSION.SDK_INT >= 26) {
    vibration.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
    vibrator.vibrate(200)
}
Ghayas
  • 1,266
  • 10
  • 17
1

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

Be sure you added permission at AndroidManifest

Seymur Mammadli
  • 1,724
  • 15
  • 13
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/12278247) – Vini.g.fer May 08 '16 at 00:24
  • 3
    @Vini.g.fer How does this not attempt to provide an answer? It might be completely wrong, [but that doesn't mean we should abuse the review queues to delete it.](http://meta.stackoverflow.com/q/287563/1849664) – Undo May 08 '16 at 01:04
1
private fun vibrateOnce(context: Context) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
       val vibratorManager =  context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
       val vib  = vibratorManager.defaultVibrator;
       vib.vibrate(VibrationEffect.createOneShot(100,1 ))
   } else {
       val vib  = context.getSystemService(VIBRATOR_SERVICE) as Vibrator
       vib.vibrate(500)
    }
}
Ramakrishna Joshi
  • 1,442
  • 17
  • 22
0

Make sure you have enabled vibration in your mobile phone. Because i turned it off. And finally realised the error is not in code. In my default settings. This code works fine.`

Manifest permission for vibration generation:

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

Add this function in onCreate on in the function your button is defined:

final Vibrator vibe = (Vibrator) yourActivity.this.getSystemService(Context.VIBRATOR_SERVICE);

Replace yourActivity.this with your own activity or if you declared a context you can write context.getSystemService(Context.VIBRATOR_SERVICE);

We can call it from anywhere:

`vibe.vibrate(80);` 

80 represents the milliseconds (the duration of the vibration)

`

shivlal kumavat
  • 868
  • 1
  • 12
  • 28