Vibrate Device Example for Android Studio
Welcome. In this tutorial you will learn how to make your android app make the device vibrate in Android Studio.
Vibrate Device Example for Android Studio
For making your device vibrate in Android Studio, we will have to use the Vibrator class. The vibrator class deals with the vibrating machine present in your device and will make it vibrate for the given duration after which it will stop.
Creating a Vibrator in Android App
First you will have to declare a vibrator object of class Vibrator as shown below:-
1 |
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); |
Then on the place when you want your device to vibrate,like inside OnClickListener of any button, just add these lines:-
1 |
vibrator.vibrate(VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE)); |
Here you use VibrationEffect.createOneShot to make a solitary vibration impact, at that point you pass the value (1000) as the length of the vibration and the VibrationEffect.DEFAULT_AMPLITUDE is used to make the device vibrate with the default settings and default strength of the device.
At any point if you want to cancel the vibration, you can simply use the vibrator.cancel() method for that purpose.
Note:- You will also need to add permission in the AndroidManifest.xml file. Simply open it and add this line:-
1 |
< uses-permission android:name="android.permission.VIBRATE" /> |
A sample image for vibrator in action is shown in the below picture. The button when clicked will make your device vibrate for 1000ms.
The Full Java Code along with the XML layout for the above example is shown below:-
- First Create a new project in Android Studio
- File ⇒ New Android ⇒ Application Project
- Then Open src -> package -> MainActivity.java and then add following code :
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import android.os.Build; import android.os.VibrationEffect; import android.os.Vibrator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class AndroidVibrate extends AppCompatActivity { Button button; Vibrator vibrator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_android_vibrate); button = findViewById(R.id.button); vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (Build.VERSION.SDK_INT >= 26) { vibrator.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE)); } else { vibrator.vibrate(200); } } }); } } |
- Now Open res -> layout -> activity_main.xml and then add following code :
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:background="#D6D2D2" android:layout_height="match_parent" tools:context=".AndroidVibrate"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Tap to vibrate" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> |
You can directly copy paste these snippets to your android studio, and they should work perfectly. Just save and run your project. You can also try to play with these other settings on your own and see the corresponding changes accordingly. There are a variety of properties available which can be changed to make your own customized vibration pattern as per your need.
Output:
Finally, run this project.
This is how Android Vibrate Device Code look like.
For a more detailed guide, you can also visit android’s official guide below:-
https://developer.android.com/reference/android/os/Vibrator
Comment down below if you are facing any problems.