Hi friends, in this article we will talk about Haptic Feedback with Xamarin Forms. First of all, let’s talk about what Haptic Feedback is. It is a vibration that iOS users actually feel constantly using the app. For example, when you purchase an application on the App Store, you will feel vibration depending on the success or failure. Even if you do not see the screen after this event somewhere, you can understand whether the result is successful or not. It creates a very pleasant feeling. It is a situation that I have not encountered so far in terms of Android users. When I search a little bit on the internet, I think we can give the buttons this feature. Instead, we can vibrate the phone ourselves, but this is not very pleasant.
First of all, we need to write Dependency Service for this. If you want to get more information about Dependency Service, you can find it here.
First of all, we need to choose which level of feedback we will give on our phone. I create an enum for this. There are 5 levels here. We can choose one of them at any level.
public enum HapticEnums { Soft, Light, Medium, Rigid, Heavy }
Then we need to create an interface. We need to write the function we will run in this interface. Here we have to say at what level the feedback will be. Since I want it to run the medium level by default, I am bringing the default value to Medium level.
public interface IHapticFeedback { void Run(HapticEnums haptic = HapticEnums.Medium); }
Then I need to use this Interface on Android and iOS. First of all, I create a class called HapticFeedback in the iOS layer and make it inherit from the interface we created in the portable layer. Then I need to write the function from this Interface. I provide and run whatever level of feedback I will provide.
[assembly:Xamarin.Forms.Dependency(typeof(HapticFeedback))] namespace ProjectName.iOS.DependencyServices { public class HapticFeedback : IHapticFeedback { public void Run(HapticEnums style = HapticEnums.Medium) { UIImpactFeedbackGenerator haptic; switch (style) { case HapticEnums.Heavy: haptic = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Heavy); break; case HapticEnums.Light: haptic = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Light); break; case HapticEnums.Medium: haptic = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Medium); break; case HapticEnums.Rigid: haptic = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Rigid); break; case HapticEnums.Soft: haptic = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Soft); break; default: haptic = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Medium); break; } haptic.ImpactOccurred(); } } }
After writing the iOS layer, we need to write the Android layer. I am not using Haptic Feedback for Android, but I will also write the case of phone shake in case you might use it. Here you can set your own level. I gave 50 milliseconds by default.
public void Run(HapticEnums haptic = HapticEnums.Medium) { Vibrator vibrator = (Vibrator)Android.App.Application.Context.GetSystemService(Android.App.Application.VibratorService); switch (haptic) { case HapticEnums.Soft: vibrator.Vibrate(50); break; case HapticEnums.Rigid: vibrator.Vibrate(50); break; case HapticEnums.Medium: vibrator.Vibrate(50); break; case HapticEnums.Light: vibrator.Vibrate(50); break; case HapticEnums.Heavy: vibrator.Vibrate(50); break; default: vibrator.Vibrate(50); break; } } }
Now we will have to run only the codes that we wrote with Dependecy Service. You can use it with Dependecy Service when we click a button or select an item from the list or in any action.
internal void RunHaptic(HapticEnums hapticEnums = HapticEnums.Medium) { DependencyService.Get<IHapticFeedback>().Run(hapticEnums); }
If you have questions, you can reach them by e-mail or comment. Good work.
Comments (2)
Joe Celinesays:
Sunday May 7th, 2023 at 04:16 AMHi,
Are you still in business?
I found a few errors on your site.
Would you like me to send over a screenshot of those errors?
Regards
Joe
(714) 908-9255
omersezersays:
Sunday July 30th, 2023 at 05:14 PMHi,
Yes currently i am working with Xamarin. If you send your problem we can solve together.