Everybody Loves Vibrating Toasts

I came across an interesting idea on stackoverflow.com for Toast notifications on Android.

It’s a simple class that extends Toast. We will modify the constructor to vibrate your device when this new Toast is displayed on screen. Below is the ~5 lined class:

public class VibratingToast extends Toast {
public VibratingToast(Context context, CharSequence text, int duration) {
super(context);

((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(300);

super.makeText(context, text, duration).show();
}
}

Now all you have to do is call your new Toast when you want some vibrations:

new VibratingToast(getApplicationContext(), "some message that needs vibrations...", Toast.LENGTH_LONG);

And, don’t forget that you will need to declare vibrating permission in your AndroidManifest.xml:

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

Easy and simple! Happy deving!!!

P.S. – if you want to get fancy and add another parameter to our constructor to accept a vibrating duration integer, please leave a comment or snippet on gist.github.com.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>