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).
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.
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.
Step 4
After opening main.axml, file will open the main page designer. You can design this page, as per your wish.
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.
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").
Step 7
Now, you will edit the textAppearance value.
(Ex: android:textAppearance="?android:attr/textAppearanceLarge").
Step 8
In this step, go to the Main.axml page Source Panel. Note, the ImageView Id value and source value.
Main.axml
- <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">
- <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" />
- </LinearLayout>
Step 9
Now, go to MainActivity.cs page and write one namespace, mentioned below.
- using Android.Hardware;
-
- Activity, ISensorEventListener
-
- static readonly object _syncLock = new object();
- SensorManager _sensorManager;
- TextView _sensorTextView;
Step 10
Now, go to the MainActivity.cs page. Write the code, mentioned below.
MainActivity.cs- public void OnAccuracyChanged(Sensor sensor, SensorStatus accuracy) {}
- public void OnSensorChanged(SensorEvent e) {
- lock(_syncLock) {
- _sensorTextView.Text = string.Format("x={0:f}, y={1:f}, y={2:f}", e.Values[0], e.Values[1], e.Values[2]);
- }
- }
- protected override void OnCreate(Bundle bundle) {
- base.OnCreate(bundle);
-
- SetContentView(Resource.Layout.Main);
- _sensorManager = (SensorManager) GetSystemService(SensorService);
- _sensorTextView = FindViewById < TextView > (Resource.Id.accelerometer_text);
- }
- protected override void OnResume() {
- base.OnResume();
- _sensorManager.RegisterListener(this,
- _sensorManager.GetDefaultSensor(SensorType.Accelerometer),
- SensorDelay.Ui);
- }
- protected override void OnPause() {
- base.OnPause();
- _sensorManager.UnregisterListener(this);
- }
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.
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.
Summary This was the process of getting Accelerometer Reading in Xamarin Android app, using Visual Studio 2015.