Home » Android » Android Vibrate on toast (Homer: Mmmm vibrate on toast)

Android Vibrate on toast (Homer: Mmmm vibrate on toast)

Posted by: admin June 15, 2020 Leave a comment


Is it possible to make the phone vibrate for ANY toast message in your program?
Or do you have to insert a vibrate command on each toast?


How to&Answers:

add this class to your code:

import android.content.Context;
import android.os.Vibrator;
import android.widget.Toast;;

public class VibratingToast extends Toast{

public VibratingToast(Context context,CharSequence text, int duration) {
    Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    super.makeText(context, text, duration).show();


and then you can call a toast by adding this line when you want to show a vibrating toast:

new VibratingToast(this, "Hi,....", Toast.LENGTH_SHORT);

You will also need, if you have’t already, to add vibration permission in your manifest file

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


You could simply subclass the Notification class and have its vibrate command initialised in the Constructor. Then instead of using the SDK Notification class, use that one each time you need to notify in your application.

public class MyNotification extends Notification {
    public MyNotification() {
        vibrate = /* Your vibration parameters here */;
        // Or to use default vibration:
        // flags = DEFAULT_VIBRATE;

Then, when you want to notify:

notificationManager.notify(new MyNotification());