Friday, 14 November 2025

How to Vibrate your phone in Android Studio

 

The shortest code to make your phone vibrate is here.

First of all write the vibrate permission in the AndroidManifest.xml file

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

Then create a button in activity_main.xml

<Button
android:id="@+id/pattern_vibration_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Trigger Pattern Vibration"/>

Now in the Java file:

Create an object of Vibrator and get the system's vibrator service

Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

Check if the device supports vibrator

if (vibrator == null || !vibrator.hasVibrator()) {
Toast.makeText(this, "Vibrator not supported on this device", Toast.LENGTH_SHORT).show();
return;
}

Create a method to perform the vibration

private void triggerPatternVibration() {
long[] pattern = {0, 200, 100, 300};

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
VibrationEffect effect = VibrationEffect.createWaveform(pattern, -1);
vibrator.vibrate(effect);
} else {
// For devices with SDK < 26
vibrator.vibrate(pattern, -1); // -1 means no repeat
}
Toast.makeText(this, "Pattern Vibration Triggered", Toast.LENGTH_SHORT).show();
}

The patterns are in milli seconds and goes in this way: wait, vibrate, pause, vibrate.

Check the Build version.

Perform the vibration only once hence use -1.

Finally let the vibration be controlled through the button we created:

Button patternVibrationButton = findViewById(R.id.pattern_vibration_button);
patternVibrationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
triggerPatternVibration();
}
});

So here we are done with our code.

------------------------------------------------------------------------------------------

The complete code:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.VIBRATE"/>

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PhoneVibrate">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<Button
android:id="@+id/pattern_vibration_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Trigger Pattern Vibration"/>

</LinearLayout>

MainActivity.java

package com.prashantsj.apps.phonevibrate;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private Vibrator vibrator;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

if (vibrator == null || !vibrator.hasVibrator()) {
Toast.makeText(this, "Vibrator not supported on this device", Toast.LENGTH_SHORT).show();
return;
}

Button patternVibrationButton = findViewById(R.id.pattern_vibration_button);
patternVibrationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
triggerPatternVibration();
}
});
}

private void triggerPatternVibration() {
long[] pattern = {0, 200, 100, 300};

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
VibrationEffect effect = VibrationEffect.createWaveform(pattern, -1); // -1 means no repeat
vibrator.vibrate(effect);
} else {
// For devices with SDK < 26
vibrator.vibrate(pattern, -1); // -1 means no repeat
}
Toast.makeText(this, "Pattern Vibration Triggered", Toast.LENGTH_SHORT).show();
}

}

No comments:

Post a Comment

ElevenLabs For Audio Models