Start stop android vibrate example tutorial

How to enable vibration on android and disable vibration dynamically on button click and SOS vibration mode.

This is the complete reference for android vibrate class. In this tutorial we are simply starting the vibration on button click the vibration is in normal without separate. Sometimes app developer needs some specific requirement like vibration in pattern like SOS message vibration. We can also stop the currently vibrating programmatically on button click using cancel() method. So here is the complete step by step tutorial for Start stop android vibrate example tutorial.

android-project-download-code-button

How to Start stop android vibrate example tutorial.

Note: Please add VIBRATE permission inside your AndroidManifest.xml file.

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

Code for MainActivity.java file.

 package com.startstopandroidvibrateexample_android_examples.com;

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

public class MainActivity extends Activity {

 Button one,two,three;
 Vibrator vibrator;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 one = (Button)findViewById(R.id.button1);
 two = (Button)findViewById(R.id.button2);
 three = (Button)findViewById(R.id.button3);
 
 vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
 
 one.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 vibrator.vibrate(7000); 
 
 }
 });
 
 two.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub

 long pattern[] = {60,120,180,240,300,360,420,480};

 vibrator.vibrate(pattern, 1);
 
 }
 });
 
 three.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 vibrator.cancel();
 
 }
 });
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 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="com.startstopandroidvibrateexample_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="130dp"
 android:text="Click Here to Vibrate" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click Here to Vibrate in Patterns like SOS" />

 <Button
 android:id="@+id/button3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button2"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="7dp"
 android:text="Stop Vibration" />

</RelativeLayout>

Screenshot:

Start stop android vibrate example tutorial

Click here to download Start stop android vibrate example tutorial project with source code.