Vibrate android phone device programmatically

How to turn on start vibration on android mobile phone and tablet dynamically on button click.

Vibrator is one of the most and useful devices for mobile phones because with the use of vibrator android phones can vibrate and on silent mode after vibrating its user will know that messages or calls are coming. So in this tutorial we are vibrating the android phone on button click. So here is the complete step by step tutorial for Vibrate android phone device programmatically.

android-project-download-code-button

How to Vibrate android phone device programmatically.

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

 

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

 

Code for MainActivity.java file.

 package com.vibrateandroidphone_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;
import android.widget.TextView;


public class MainActivity extends Activity {

 TextView textview;
 Button button;
 Vibrator vibrator;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview = (TextView)findViewById(R.id.textView1);
 button = (Button)findViewById(R.id.button1);
 
 vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 vibrator.vibrate(7000);
 
 textview.setText(" Device is Vibrating ");
 
 }
 });
 }
}

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.vibrateandroidphone_android_examples.com.MainActivity" >

 <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="148dp"
 android:text=""
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="34dp"
 android:text="Click here to Vibrate android phone device programmatically" />

</RelativeLayout>

Screenshot:

Vibrate android phone device programmatically

Click here to download Vibrate android phone device programmatically project with source code.

Leave a Reply

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