Device Info In Android Using Xamarin.Forms

Introduction
 
This article demonstrates how to retrieve a device info in Android using Xamarin.Forms. Xamarin is a platform that allows us to create a multi-platform mobile application for platforms, like Android, Windows, iOS through a single integrated development environment (IDE). And with Xamarin.Forms, the interface design for all three platforms can be accomplished within its XAML-based framework. Xamarin apps are built with standard, native user-interface control.
 
 
 
Step 1  
 
Open Visual Studio and go to New Project >> Installed >> Visual C# >> Cross-Platform.
 
Select Cross-Platform app, then give your project a name and location, and click "OK" button.
 
  
Step 2 
 
Next, go to Project Name >> right click >> Manage NugGet Packages for Solution. Select the "Manage NuGet package" option.
 
 
 
Now, search and select the following NuGet package and select your project to install on it.
  • Xam.Plugin.DeviceInfo
 
 
Step 3 
 
Next, add an image to Solution Explorer >> Project Name.Android>> Resources >> Right Click >>  Drawable >> Add >> Existing Item. When we click an existing item button, it opens a dialog box. 
 
 
 
Choose image location and add images in this new window.
 
 
 
The Images are added successfully. Then, move the cursor to that image and just verify the image. 
 
 
 
Step 4 
 
Open Solution Explorer >> Project Name(Portable) >> MainPage.xaml. Double click for opening the design view of this page.
 
 
 
The Code is given below just copy it.
 
 
 
XAML Code 
 
We are creating Labels, texts, and fontattribues inside the stacklayout. And, we add a background image inside the content page. Label names are "ID", "IDIOM", "MODEL", "PLATFORM", "VERSION".
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:Device_Info"  
  5.              x:Class="Device_Info.MainPage"  
  6.              BackgroundImage="Android.png">  
  7.     <ContentPage.Content>  
  8.         <StackLayout>  
  9.             <Label Text="ID"  
  10.                    x:Name="Id"  
  11.                    VerticalOptions="Center"  
  12.                    HorizontalOptions="Center"  
  13.                    FontAttributes="Bold"/>  
  14.             <Label Text="IDIOM"  
  15.                    x:Name="Idi"  
  16.                    VerticalOptions="Center"  
  17.                    HorizontalOptions="Center"  
  18.                    FontAttributes="Bold"/>  
  19.             <Label Text="MODEL"  
  20.                    x:Name="Mdl"  
  21.                    VerticalOptions="Center"  
  22.                    HorizontalOptions="Center"  
  23.                    FontAttributes="Bold"/>  
  24.             <Label Text="PLATFORM"  
  25.                    x:Name="PLF"  
  26.                    VerticalOptions="Center"  
  27.                    HorizontalOptions="Center"  
  28.                    FontAttributes="Bold"/>  
  29.             <Label Text="VERSION"  
  30.                    x:Name="Vs"  
  31.                    VerticalOptions="Center"  
  32.                    HorizontalOptions="Center"  
  33.                    FontAttributes="Bold"/>  
  34.         </StackLayout>  
  35.     </ContentPage.Content>  
  36. </ContentPage>  
Step 5
 
Open Solution Explorer >> Project Name (Portaqble) >> MainPage.xaml.cs. Double-click for opening the design of this page.
 
 
 
The code is given below.
 
 
 
C# Code 
 
This code is able to work for Deviceinfo Plugin using Id, idiom, model, and platform, version.
  1. using Plugin.DeviceInfo;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using Xamarin.Forms;  
  8.   
  9. namespace Device_Info  
  10. {  
  11.     public partial class MainPage : ContentPage  
  12.     {  
  13.         public MainPage()  
  14.         {  
  15.             InitializeComponent();  
  16.             getDeviceInfo();  
  17.         }  
  18.   
  19.         private void getDeviceInfo()  
  20.         {  
  21.             Id.Text = CrossDeviceInfo.Current.Id;  
  22.   
  23.             Idi.Text = CrossDeviceInfo.Current.Idiom.ToString();  
  24.   
  25.             Mdl.Text = CrossDeviceInfo.Current.Model;  
  26.   
  27.             PLF.Text = CrossDeviceInfo.Current.Platform.ToString();  
  28.   
  29.             Vs.Text = CrossDeviceInfo.Current.Version;  
  30.         }  
  31.     }  
  32. }  
Step 6 
 
Next, select the Built & Deploy option followed by selecting from the list of Android Emulator or simulator. You can choose any API (Application Program Interface) Level Emulator or simulator to run it.
 
Output 
 
After a few seconds, you will see your app working. The current device info result is displayed.
 
 
 
Finally, we have successfully created Xamarin.Forms application.
 
Conclusion 
 
I hope you have learned how to retrieve the device info in Android using Xamarin.Forms with Visual Studio and C#.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now