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.
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".
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:local="clr-namespace:Device_Info"
- x:Class="Device_Info.MainPage"
- BackgroundImage="Android.png">
- <ContentPage.Content>
- <StackLayout>
- <Label Text="ID"
- x:Name="Id"
- VerticalOptions="Center"
- HorizontalOptions="Center"
- FontAttributes="Bold"/>
- <Label Text="IDIOM"
- x:Name="Idi"
- VerticalOptions="Center"
- HorizontalOptions="Center"
- FontAttributes="Bold"/>
- <Label Text="MODEL"
- x:Name="Mdl"
- VerticalOptions="Center"
- HorizontalOptions="Center"
- FontAttributes="Bold"/>
- <Label Text="PLATFORM"
- x:Name="PLF"
- VerticalOptions="Center"
- HorizontalOptions="Center"
- FontAttributes="Bold"/>
- <Label Text="VERSION"
- x:Name="Vs"
- VerticalOptions="Center"
- HorizontalOptions="Center"
- FontAttributes="Bold"/>
- </StackLayout>
- </ContentPage.Content>
- </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.
- using Plugin.DeviceInfo;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
-
- namespace Device_Info
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- getDeviceInfo();
- }
-
- private void getDeviceInfo()
- {
- Id.Text = CrossDeviceInfo.Current.Id;
-
- Idi.Text = CrossDeviceInfo.Current.Idiom.ToString();
-
- Mdl.Text = CrossDeviceInfo.Current.Model;
-
- PLF.Text = CrossDeviceInfo.Current.Platform.ToString();
-
- Vs.Text = CrossDeviceInfo.Current.Version;
- }
- }
- }
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#.