Retrieving Device Display Information In Xamarin Forms Application Using Xamarin Essentials For Android And UWP

The DeviceDisplay class is available at Xamarin.Essentials API. It provides information about the device's screen metrics the application is running on. Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all in C# leveraging Xamarin. Xamarin.Essentials provide a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.

Reading this article, you can learn Retrieving Device Display Information like device orientation, rotation, width, height, density in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in cross-platform application development.

The following important tools are required for developing Xamarin Forms App,

  1. Windows 10 (Recommended)
  2. Visual Studio 2017
  3. Android API 19 or higher and UWP 10.0.16299.0 or higher.

Now, we can discuss step by step app development.

Step 1

Open Visual Studio 2017 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Mobile App (Xamarin.Forms)-> Give the Suitable Name for your App (XamFormDevDispinfo) ->OK

Retrieving Device Display Information in Xamarin Forms application using Xamarin Essentials for Android and UWP 

Step 2

Select the cross-platform template as a Blank APP ->Set Platform as Android and UWP and code sharing strategy as .NET standard, Afterwards, Visual Studio creates 3 projects (Portable, Droid, UWP)

Retrieving Device Display Information in Xamarin Forms application using Xamarin Essentials for Android and UWP 

Step 3

For adding a reference,

RightClick your solution (XamFormDevDispinfo) and Select Manage NuGet Packages.

For adding Xamarin.Essentials Reference, choose browse and search Xamarin.Essentials, select the package and select all the projects (portable, Android, UWP) and install it.

Retrieving Device Display Information in Xamarin Forms application using Xamarin Essentials for Android and UWP 

Step 4

Add the Label controls in Mainpage.Xaml for displaying Title and Device orientation, rotation, width, height, density.

  1. <Label FontAttributes="Bold" Text="Retrieving Device Display info in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>  
  2. <Label x:Name="lblOrient" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>  
  3. <Label x:Name="lblRot" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>  
  4. <Label x:Name="lblWidth" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>  
  5. <Label x:Name="lblHeight" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>  
  6. <Label x:Name="lblDens" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label> 

Step 5

Add the following code in the MainActivity.cs in XamFormDevDispinfo.Android project,

In onCreate() method,

  1. Xamarin.Essentials.Platform.Init(this, savedInstanceState);  

Also add the below method,

  1. publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)  
  2. {  
  3.    Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  4.    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  5. }  

Step 6

Add the following namespace and code in Mainpage.Xaml.cs

  1. using Xamarin.Essentials;  
  2. var metrics = DeviceDisplay.ScreenMetrics;  
  3. lblOrient.Text = "Orientation : " + metrics.Orientation.ToString();  
  4. lblWidth.Text = "Width : " + metrics.Width.ToString();  
  5. lblHeight.Text = "Height : " + metrics.Height.ToString();  
  6. lblDens.Text = "Density : " + metrics.Density.ToString();  
  7. lblRot.Text = "Rotation : " + metrics.Rotation.ToString();  
Note
Automatically the following code will be generated in XAML code view, while we are done in the design view.
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamFormDevDispinfo" x:Class="XamFormDevDispinfo.MainPage">  
  3.     <StackLayout>  
  4.         <Label FontAttributes="Bold" Text="Retrieving Device Display info in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />  
  5.         <Label x:Name="lblOrient" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>  
  6.         <Label x:Name="lblRot" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>  
  7.         <Label x:Name="lblWidth" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>  
  8.         <Label x:Name="lblHeight" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>  
  9.         <Label x:Name="lblDens" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>  
  10.     </StackLayout>  
  11. </ContentPage>  
Step 7

We will test Android and UWP. So, we can set the Multiple Startup Projects as XamFormDevDispinfo.Droid and XamFormDevDispinfo.UWP (Universal Windows).

Retrieving Device Display Information in Xamarin Forms application using Xamarin Essentials for Android and UWP 

Step 8

Deploy your App in UWP and Android. The output of the XamFormDevDispinfo app is,

Retrieving Device Display Information in Xamarin Forms application using Xamarin Essentials for Android and UWP
Summary

Now, you have successfully retrieved the Device Display information in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.


Similar Articles