How to Vibrate a Device Programmatically in Android?
Hepatic feedbacks are also considered when it comes to user experience. So in this discussion, it’s been discussed various types of haptics or the types of vibration of the device. For example, click haptics or long-press button haptics. There five different types of vibration modes in haptic feedback discussed are:
- Default vibration of the device
- Click effect vibration
- Double click effect vibration
- Heavy click effect vibration
- Tick effect vibration
Note that we are going to implement this project using the Java language.
Steps to implement haptic feedbacks in Android
Step 1: Create an empty activity Android studio project
- Create an empty activity android studio project.
- Refer to Android | How to Create/Start a New Project in Android Studio?
- Note that select Java as the programming language.
Step 2: Working with the activity_main.xml
- In this discussion, four different types of haptics are discussed.
- So to generate that haptics there are four different buttons are included in the layout. Invoke the following code inside the activity_main.xml file.
- Make sure to give appropriate IDs for all the buttons to handle them in the MainActivity.java file.
Step 3: Invoking Vibrate permission in AndroidManifest file
The vibration of the device needs permissions. To invoke the following code inside the AndroidManifest file.
Step 4: Working with the MainActivity.java file
- As it is said that there are five different kinds of vibrations. For those five kinds of vibrations, there are constants for each of them. Those are:
DEFAULT_AMPLITUDE -> for default vibration of the device
EFFECT_CLICK -> for single click haptic
EFFECT_DOUBLE_CLICK -> for double click of the view
EFFECT_HEAVY_CLICK -> for heavy click effect of the view
EFFECT_TICK -> for tick effect vibration
- Invoke the following code inside the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
The output should be tested on the physical android device. To know how to set up a physical android studio refer to How to Run the Android App on a Real Device?
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.