how to make phone vibrate android studio with code examples

As a developer, it's often important to give users feedback when an action has been performed. One popular way to do this is by making the phone vibrate. In this article, I'll show you how to make the phone vibrate in Android Studio with code examples.

Step 1: Adding Permissions

Before we can start making the phone vibrate, we need to add permission to the manifest file. Here's how you can do it:

  1. Open the AndroidManifest.xml file in your project.
  2. Paste the following code inside the manifest tags:
<uses-permission android:name="android.permission.VIBRATE" />

This allows your app to use the vibration feature of the phone.

Step 2: Create the Vibration Pattern

The next step is to create the vibration pattern that your app will use. You can create this pattern using an array of long values. Each value in the array represents the duration, in milliseconds, of a vibration. If you want a single quick vibration, the pattern would look like this:

long[] pattern = {0, 500};

This pattern means that there will be no vibration for 0 milliseconds, followed by a 500 millisecond vibration.

If you want a longer vibration, you can adjust the values in the pattern array accordingly. You can also add multiple values to the array if you want more complex vibration patterns.

Step 3: Vibrate the Phone

Now that we have our pattern, we can use it to vibrate the phone. Here's how you can do it:

  1. Create a Vibrator object:
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
  1. Check if the phone has vibration capabilities:
if (vibrator.hasVibrator()) {
   // Vibrate the phone
  1. Vibrate the phone using the pattern we created earlier:
vibrator.vibrate(pattern, -1);

The -1 parameter specifies whether the vibration should repeat. A value of -1 means that the vibration should not repeat.

Putting It All Together

Here's an example of how to use the code we just covered:

// Create the vibration pattern
long[] pattern = {0, 500};

// Create a Vibrator object
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Check if the phone has vibration capabilities
if (vibrator.hasVibrator()) {
   // Vibrate the phone
   vibrator.vibrate(pattern, -1);

This code will create a single quick vibration if the phone has vibration capabilities.


Making the phone vibrate is a useful feature that can enhance the user experience of your app. By following the steps outlined in this article, you should be able to make the phone vibrate in Android Studio with ease. Remember to add the necessary permissions to the manifest file, create a vibration pattern, and use the Vibrator object to vibrate the phone.

  1. Adding Permissions:

Adding permissions in Android is crucial when you're implementing sensitive/useful features like accessing user contacts, camera, storage, and others. It helps the user decide whether or not the app is trustworthy enough to grant the permission. Without permission, your app might malfunction or even crash. Whenever your app requires some essential functionality to work correctly, make sure to add the permission for it. The user is less likely to trust your app if it doesn't mention the permission required.

  1. Creating a Vibration Pattern:

As a developer, you have the freedom to control the vibration pattern and duration in your app. Depending on your app's context, you might want to create a custom pattern that users will easily identify. For example, for notifications, you might want to create a long vibration pattern, and for alerts, you might want to create a short, rapid pattern.

You can use the array of longs values which represent the vibration duration to create a custom pattern. For example, if you want a pattern that goes like short-short-long-short, you can create an array with duration such as {0,200,50,1000,100,300,400,2000}. The array follows the format of ‘wait-duration’ – where the first value (the wait value) in the array says how long to wait before the first vibration, and all subsequent values in the array alternate between the length of the vibration in milliseconds and the length of the wait to the next.

  1. Vibrating the phone:

Vibrating the phone is simple, and after successfully creating the vibration pattern, you can use that pattern to vibrate the phone. Android provides the Vibrator class, which you can instantiate and take advantage of the method vibrate() to make the phone vibrate.

You can pass your vibration pattern that you created using an array of long to the vibrate() method which accepts two parameters; 1st is the vibration pattern you wish to create, and 2nd is the position at which you want the vibration pattern to start from. A value of -1 in the second parameter indicates that it should start vibrating from the beginning.

Keep in mind, the permission you added in the manifest is required to make the phone vibrate. If the user has denied this permission and you try to make a phone vibrate, you might end up with a SecurityException.

Popular questions

  1. Why is it important to add permissions before making the phone vibrate in Android Studio?
    Answer: It's important to add permissions because permissions are required for accessing the phone's hardware features, such as vibration. Without the required permission, the app might not function correctly or even crash.

  2. Can you create a custom vibration pattern in Android Studio?
    Answer: Yes, you can create a custom vibration pattern in Android Studio. You can use an array of long values representing the duration of the vibration to create a pattern.

  3. How can you make the phone vibrate in Android Studio?
    Answer: To make the phone vibrate in Android Studio, you can use the Vibrator class and the vibrate() method. You need to pass the vibration pattern you created to this method to make the phone vibrate.

  4. What happens if the user denies the permission to vibrate the phone?
    Answer: If the user denies the permission to vibrate the phone, the application cannot make the phone vibrate. It's important to handle this case gracefully to prevent the app from crashing.

  5. Is adding permission required for every device running the app?
    Answer: Yes, adding permission is required for every device running the app. Without the permission added to the manifest file, the app will not have access to the phone's hardware features, such as vibration.



As an experienced software engineer, I have a strong background in the financial services industry. Throughout my career, I have honed my skills in a variety of areas, including public speaking, HTML, JavaScript, leadership, and React.js. My passion for software engineering stems from a desire to create innovative solutions that make a positive impact on the world. I hold a Bachelor of Technology in IT from Sri Ramakrishna Engineering College, which has provided me with a solid foundation in software engineering principles and practices. I am constantly seeking to expand my knowledge and stay up-to-date with the latest technologies in the field. In addition to my technical skills, I am a skilled public speaker and have a talent for presenting complex ideas in a clear and engaging manner. I believe that effective communication is essential to successful software engineering, and I strive to maintain open lines of communication with my team and clients.
Posts created 2501

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top