How To Get Mobile Phone Screen Size 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, given below are required to be followed in order to get the mobile phone screen size 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.

Now, select Resource-->Layout-->double click to open main.axml page. You need to select the source to write XAML code.

If you want the design, choose the designer Window and you can design your app.

project

Step 4

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

project

Now, delete the Linear Layout and 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, now delete C# button action code.

Go to the 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. Now, scroll down and you will see all the tools and controls.

You need to drag and drop the two TextViews.

project

Step 6

Now, go to the properties Window. You need to edit the first TextView Id value (android:id="@+id/screenWidthDp").

project

Step 7

You need to edit the second TextView Id value (Ex: android:id="@+id/screenHeightDp").

project

Step 8

In this step, go to the Main.axml page Source Panel. Note, the TextView Id values.

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">  
  2.     <TextView android:text="Screen Width:" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/screenWidthDp" />  
  3.     <TextView android:text="Screen Height:" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/screenHeightDp" />  
  4. </LinearLayout>  
Step 9

Now, go to the MainActivity.cs page. Write the code, given below between the OnCreate() Method.

project

MainActivity.cs
  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     // Set our view from the "main" layout resource  
  4.     SetContentView(Resource.Layout.Main);  
  5.     var metrics = Resources.DisplayMetrics;  
  6.     var widthInDp = ConvertPixelsToDp(metrics.WidthPixels);  
  7.     var heightInDp = ConvertPixelsToDp(metrics.HeightPixels);  
  8.     FindViewById < TextView > (Resource.Id.screenWidthDp).Text = "Screen Width: " + widthInDp + " dp.";  
  9.     FindViewById < TextView > (Resource.Id.screenHeightDp).Text = "Screen Height: " + heightInDp + " dp.";  
  10. }  
  11. private int ConvertPixelsToDp(float pixelValue) {  
  12.     var dp = (int)((pixelValue) / Resources.DisplayMetrics.Density);  
  13.     return dp;  
  14. }  
Step 10

If you have an 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 the Run option.

project

Output

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

You will see the screen size width and height.

project

Step 11

You will connect another Android phone and run this app (Ex. sony E2043(Android 4.4 -API 19).

project

Output 2


You need to see the screen size width and height in your app running phone (Ex. Sony).

project

Summary

This was the process of how to get the mobile phone screen size in Xamarin Android app, using Visual Studio 2015. 


Similar Articles