Android Tutorial on Vibration | How to vibrate device on button click

 Introduction :

Android has got a lot of features which all of us know and in every tutorial i am using one of it to make our app and this time i have brought a new tutorial topic vibration which you might have experienced a lot of times.

People use this feature to avoid loud sound or music when we receive a phone call or a message or any other device broadcast we may use silent but we will not get notified.

Vibration mode is useful in these kinds of situations by alerting user regarding the incoming call or any other services like notifications, messages…

Yes, you are right vibration is not only a feature of android but you can also find it in many basic devices.Here, we are going to see how to implement it in android. 

Android Tutorial on Vibration :

Generally vibration mode is used to notify user regarding any message, phone call, when we deactivate sounds i.e., ringtones and also for any other notifications which may include alert / error messages.

So here i think i now you got a basic knowledge regarding vibration mode, or uses of vibration, so we can move forward.

Note :  Android vibration tutorial is updated because of the latest android changes in the new versions starting from Api 26 and i have provided the code below after the previous code do go through it and let me know in comment section if any help required.


Creating activity_vibrate.xml :

 

Its a simple interface where just we require a button.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">


    <Button
        android:id="@+id/vibrate"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginTop="50dp"
        android:background="@drawable/vibrate" />


</LinearLayout>

 

Creating a VibrateActivity.java :

Here we will be using vibrator service which is a part of system services in android.

Vibrator = (Vibrator)getSystemService(MainActivity.VIBRATOR_SERVICE);

 

package vibrator.androidcoding.abhishek.vibrator;

import android.app.Activity;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;

import vibrator.androidcoding.abhishek.vibrator.R;

public class MainActivity extends Activity {

    Button buttonVibrate;

    Vibrator Vibrator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonVibrate = (Button)findViewById(R.id.vibrate);


        /*Vibrator = (Vibrator)getSystemService(MainActivity.VIBRATOR_SERVICE);*/

        buttonVibrate.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                /*Vibrator.vibrate(1000);*/
                 vibrate();

            }});

    }

// new code update which latest devices supports old and new devices
private void vibrate() {

if (Build.VERSION.SDK_INT >= 26) {
        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(1000,10));
    } else {
        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(1000);
    }
    
}
}

 

AndroidManifest.xml :

We need to add a permission for vibration and this is the crucial step as without permission your app may not be able to make device vibrate. 

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

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="vibrator.androidcoding.abhishek.vibrator" >

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

 

Output :

This screen depicts the Android Tutorial on Vibration

 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

How to extract filename from Uri?

Now, we can extract filename with and without extension :) You will convert your bitmap to uri and get the real path of your file. Now w...