Check android phone supports vibrate programmatically

How to find out android mobile phone device has a vibrator and support vibration dynamically on button click.

In this tutorial we are simply checking on button click that our android device has a vibrator or not. So here is the complete step by step tutorial for Check android phone supports vibrate programmatically.

How to Check android phone supports vibrate programmatically.

android-project-download-code-button

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

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

Code for MainActivity.java file.

 package com.checkandroidphonesupportsvibrate_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
 
 if(vibrator.hasVibrator()){
 
 textview.setText("Device supports Vibrator.");
 }
 else {
 
 textview.setText("Device Not supports Vibrator.");
 
 }
 }
 });
 
 }

}

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.checkandroidphonesupportsvibrate_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="157dp"
 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="21dp"
 android:text="Click here to Check android phone supports vibrate programmatically" />

</RelativeLayout>

Screenshot:

Check android phone supports vibrate programmatically

Click here to download Check android phone supports vibrate programmatically project with source code.

Leave a Reply

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