Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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)
}

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

Be sure you added permission at AndroidManifest

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)

`

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)
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top