How To Vibrate Android Device On Button Click Using Vibrator Effects Using Kotlin?
How to vibrate an Android device coding with Kotlin when pressing any buttons? I have used this code below, but there aren't any effects or vibrations performed. //click listener
Solution 1:
You can create a fun
and use from it (Kotlin
):
fun Fragment.vibratePhone() {
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
vibrator.vibrate(200)
}
}
And in your fragment
:
vibratePhone()
Finally in you manifest
:
<uses-permissionandroid:name="android.permission.VIBRATE" />
Solution 2:
Answer given by @R2R is perfect. But vibrate()
method of Vibrator
class is deprecated from API Level 26. So, I am giving you the updated code:
You don't have to change everything. Just update the code for vibration in MainActivity.kt.
MainActivity.kt:
classMainActivity : AppCompatActivity() {
overridefunonCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main);
val btn_click_me = findViewById(R.id.btn_vibrate) as Button
btn_click_me.setOnClickListener {
val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (vibrator.hasVibrator()) { // Vibrator availability checkingif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)) // New vibrate method for API Level 26 or higher
} else {
vibrator.vibrate(500) // Vibrate method for below API Level 26
}
}
}
}
}
Solution 3:
Vibration using kotlin working sample try this
Manifest.xml
<uses-permissionandroid:name="android.permission.VIBRATE" />
In kotlin:
val vibratorService = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibratorService.vibrate(500)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"
><Buttonandroid:id="@+id/btn_vibrate"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Vibrate"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>
MainActivity.kt
classMainActivity : AppCompatActivity() {
overridefunonCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main);
val btn_click_me = findViewById(R.id.btn_vibrate) as Button
btn_click_me.setOnClickListener {
val vibratorService = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibratorService.vibrate(500)
}
}
}
Solution 4:
Simplify it could be like
val buzzer = activity?.getSystemService<Vibrator>()
buzzer?.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
buzzer.vibrate(VibrationEffect.createWaveform(pattern, -1))
} else {
//deprecated in API 26
buzzer.vibrate(pattern, -1)
}
}
When pattern could be like
valpattern= longArrayOf(0, 200, 100, 300)
Elements of buzzing and non-buzzing takes -> wait 0 ms, buzz 200ms, wait 100ms, buzz 300ms
Of course with permission in Manifest
<uses-permissionandroid:name="android.permission.VIBRATE" />
Hope, it will help somebody in future
Post a Comment for "How To Vibrate Android Device On Button Click Using Vibrator Effects Using Kotlin?"