Get Accelerometer Reading In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS).

In the Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, mentioned below are required to be followed in order to Get Accelerometer Reading in the Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio or click (Ctrl+Shift+N).

Project

Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app; a name (Ex:sample) and give the path of your project. Afterwards, click OK.

Project

Step 3

Now, go to Solution Explorer. In Solution Explorer, get all the files and sources in your project.

Select Resource-->Layout-->double click to open main.axml page. To write XAML code, you need to select source.

Choose the Designer Window, if you want to design, and you can design your app.

Project

Step 4

After opening main.axml, file will open the main page designer. You can design this page, as per your wish.

Project

Now, delete the Default hello world button. Go to the source panel and you can see the button coding. You need to delete it.

After deleting XAML code, delete C# button action code.

Go to MainActivity.cs page. You need to delete the button code.

Step 5

Now, go to the toolbox Window. In the toolbox Window, get all the types of the tools and controls.

You need to go to the toolbox Window and scroll down subsequently. You will see all the tools and controls.

You need to drag and drop the TextView.

Project

Step 6

Now, go to the properties Window. You need to edit the TextView Id value and Text value.

(EX:android:id="@+id/textView1" android:text="@string/blur_radius_text").

Project

Step 7

Now, you will edit the textAppearance value.

(Ex: android:textAppearance="?android:attr/textAppearanceLarge").

Project

Step 8

In this step, go to the Main.axml page Source Panel. Note, the ImageView Id value and source value.

Project

Main.axml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px">  
  2.     <TextView android:id="@+id/accelerometer_text" android:layout_width="fill_parent" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:text="TEXT" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="30dp" />  
  3. </LinearLayout>  
Step 9

Now, go to MainActivity.cs page and write one namespace, mentioned below.
  1. using Android.Hardware;  
  2. //Create a sub class  
  3. Activity, ISensorEventListener  
  4. //create some variables here  
  5. static readonly object _syncLock = new object();  
  6. SensorManager _sensorManager;  
  7. TextView _sensorTextView;  
project

Step 10

Now, go to the MainActivity.cs page. Write the code, mentioned below.

MainActivity.cs
  1. public void OnAccuracyChanged(Sensor sensor, SensorStatus accuracy) {}  
  2. public void OnSensorChanged(SensorEvent e) {  
  3.     lock(_syncLock) {  
  4.         _sensorTextView.Text = string.Format("x={0:f}, y={1:f}, y={2:f}", e.Values[0], e.Values[1], e.Values[2]);  
  5.     }  
  6. }  
  7. protected override void OnCreate(Bundle bundle) {  
  8.     base.OnCreate(bundle);  
  9.     // Set our view from the "main" layout resource  
  10.     SetContentView(Resource.Layout.Main);  
  11.     _sensorManager = (SensorManager) GetSystemService(SensorService);  
  12.     _sensorTextView = FindViewById < TextView > (Resource.Id.accelerometer_text);  
  13. }  
  14. protected override void OnResume() {  
  15.     base.OnResume();  
  16.     _sensorManager.RegisterListener(this,  
  17.         _sensorManager.GetDefaultSensor(SensorType.Accelerometer),  
  18.         SensorDelay.Ui);  
  19. }  
  20. protected override void OnPause() {  
  21.     base.OnPause();  
  22.     _sensorManager.UnregisterListener(this);  
  23. }  
accelerometer

Step 11

If you have Android Virtual device, run the app on it, else connect your Android phone and run the app on it.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu

(Ex:LENOVO A6020a40(Android 5.1-API 22)). Click Run option.

accelerometer

Output

After a few seconds, the app will start running on your phone.

You will see the accelerometer to measure a device’s motion in three dimensions successfully.

accelerometer

Summary

This was the process of getting Accelerometer Reading in Xamarin Android app, using Visual Studio 2015.

 


Similar Articles