Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vibrate onclick

Tags:

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.

like image 877
Rui Miranda Avatar asked Nov 22 '13 16:11

Rui Miranda


People also ask

How to vibrate on button Click in Android?

You can turn on vibration for ringing, notifications, and touch. Open your device's Settings app . Tap Accessibility. Tap Vibration & haptic strength.

How do you vibrate in Javascript?

Starting the Vibration The device can be vibrated using the navigator. vibrate() method. Single vibration : To perform a single vibration, an integer can be passed as the parameter. This represents the time in milliseconds for which device will vibrate.


2 Answers

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.

like image 81
Bryan Herbst Avatar answered Sep 18 '22 15:09

Bryan Herbst


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) } 
like image 28
Ghayas Avatar answered Sep 18 '22 15:09

Ghayas