Xamarin Essentials Vibration

The vibration feature inside a mobile application is a very important thing to notify something to the user, so today we're going to learn how to use this feature.

First we need to Open our Essentials Solution.

Go to Essentials.Android proyect, right click and select Properties, then at Android Manifest search for VIBRATE permission and check it.

After that at Portable proyect > Views create a new ContentPage with the name: VibrationPage

Replace the content with this code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
                    x:Class="Essentials.Views.VibrationPage"
                    xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
                    <ContentPage.Content>
                    <StackLayout>
                    <Label
                    x:Name="secondsLabel"
                    Margin="0,100"
                    HorizontalOptions="Center"
                    Text="0 Seconds"
                    VerticalOptions="Center" />
                    <Stepper
                    x:Name="secondsStepper"
                    HorizontalOptions="Center"
                    ValueChanged="Stepper_ValueChanged"
                    VerticalOptions="Center" />
                    <Button
                    Margin="0,0,0,100"
                    BackgroundColor="{StaticResource NavigationPrimary}"
                    Clicked="Button_Clicked"
                    CornerRadius="30"
                    HorizontalOptions="CenterAndExpand"
                    Text="Click to Vibrate"
                    VerticalOptions="CenterAndExpand"
                    WidthRequest="150" />
                    </StackLayout>
                    </ContentPage.Content>
</ContentPage>

Replace all the Content in VibrationPage.xaml.cs with this code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Essentials.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
                    public partial class VibrationPage : ContentPage
                    {
                    public VibrationPage()
                    {
                    InitializeComponent();
                    }

                    private void Stepper_ValueChanged(object sender, ValueChangedEventArgs e)
                    {
                    secondsLabel.Text = $"{e.NewValue} Seconds";
                    }

                    private void Button_Clicked(object sender, System.EventArgs e)
                    {
                    try
                    {
                    if (secondsStepper.Value == 0)
                    {
                    Vibration.Vibrate();
                    }
                    else
                    {
                    Vibration.Vibrate(TimeSpan.FromSeconds(secondsStepper.Value));
                    }
                    }
                    catch (FeatureNotSupportedException ex)
                    {
                    // Feature not supported on device
                    }
                    catch (Exception ex)
                    {
                    // Other error has occurred.
                    }
                    }
                    }
}

Finally just search in AppShell.xaml file this lines of code:

1
2
3
    <ShellItem Title="Geolocation" FlyoutIcon="{ext:ImageResource Essentials.Resources.Images.FlyoutIcon.png}">
                    <ShellContent ContentTemplate="{DataTemplate local:GeolocationPage}" />
                    </ShellItem>

Add above that code this:

1
2
3
    <ShellItem Title="Vibration" FlyoutIcon="{ext:ImageResource Essentials.Resources.Images.FlyoutIcon.png}">
                    <ShellContent ContentTemplate="{DataTemplate local:VibrationPage}" />
                    </ShellItem>

That's all, now you can run the application, at the beginning the vibration will be the default for the system, if you click on Stepper it'll change the duration of vibration, so play with it.

Related Links

Xamarin Essentials Vibration Documentation

Completed Solution Essentials

Share this post