Android Vibrator Example



In this example we will learn how to make your phone to vibrate. Vibrate option is handy to providing users haptic feedback like when the phone rings,when you receive a notification may it be a message or anything. Here we will provide an example, where the phone vibrates for the given amount of time on a button press.

Android provides an abstract class Vibrator which is helpful in vibrating the device by obtaining an instance of the system vibrator with getSystemService(String) with constant VIBTRATOR_SERVICE as argument.

So lets start:

1.Create an android application project with any package and project name. Name the activity as VibratorExample and the layout file as activity_vibrator.
2.So let us design the UI for the activity with a TextView and a Button

activity_vibrator.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Vibrator" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="124dp"
android:text="Welcome to AndroidBite !!!"
android:textColor="@android:color/black"
android:textSize="20sp" />

<Button
android:id="@+id/bPressMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Press Me !!!" />

</RelativeLayout>

3.Now in the activity file create an instance for the vibrator class and call the method vibrate with the instance for the specified duration say 1000 milliseconds.

VibratorExample.java:

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class VibratorExample extends Activity {

 Button bPressMe;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_vibrator);
  bPressMe = (Button) findViewById(R.id.bPressMe);
  bPressMe.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {

    Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(1000);
   }
  });
 }

}


4. Add required permissions in the manifest file for vibration to occur

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

5.Now run the application by right clicking the project Run As->Android Application. The output will be similar to the one below. The phone vibrates for 1 sec when the button is pressed.

Output: