INTRODUCTION

This article demonstrates Android Application Authentications Using Xamarin.Forms. Xamarin is a platform that allows us to create a multi-platform app for Android, Windows, or iOS through a single integrated development environment (IDE). And with Xamatin.Forms, the interface design for all three platforms can be accomplished within its XAML-based standard, native user-interface controls.
Let's go.
Step 1
Open Visual Studio and go to New Project >> Installed >> Visual C# >> Cross-Platform.
Select the cross-platform app, then give your project a name and location.
Android Application Aunthetications Using Xamarin.Forms
Step 2
Install the following NuGet package to your project.
Android Application Aunthetications Using Xamarin.Forms
Update all existing packages.
Android Application Aunthetications Using Xamarin.Forms
And install the new packages.
  • Xamarin.Essentials
Android Application Aunthetications Using Xamarin.Forms
Now, select the following NuGet packages and select your project to install the package.
Step 3
Next, add an image to Solution Explorer >> Project Name.Android >> Resources >> Right Click >> drawable >> Add >> Existing Item.
Android Application Aunthetications Using Xamarin.Forms
Next, a dialogue box will open. Choose image location and add images.
Android Application Aunthetications Using Xamarin.Forms
Step 4
The Imae is added successfully for Android.
Android Application Aunthetications Using Xamarin.Forms
Step 5
Now, Open Solution Explorer >> Project Name >> MainPage.xaml. Open the design view of this page.
Android Application Aunthetications Using Xamarin.Forms
The code is given below.
Android Application Aunthetications Using Xamarin.Forms
XAML Code
  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:Permission"
  5. x:Class="Permission.MainPage">
  6. <StackLayout>
  7. <Image Source="download.png"/>
  8. </StackLayout>
  9. </ContentPage>
Step 6
Next, go to ProjectName.Android >> properties.
Android Application Aunthetications Using Xamarin.Forms
Open the design page.
In the MainActiviy.cs page in required permissions clicks on Android Manifest to click on the checkbox.
Android Application Aunthetications Using Xamarin.Forms
Choose Android Manifest and for the required permission select on "LOCATION, CAMERA, CONTACTS, MEDIA FILES" and etc..,
Android Application Aunthetications Using Xamarin.Forms
Step 7
Now, Open Solution Explorer >> Project Name.Android >> MainActivity.csl. Open the design view of this page.
Android Application Aunthetications Using Xamarin.Forms
The code is given below.
Android Application Aunthetications Using Xamarin.Forms
C# Code
  1. using System;
  2. using Android.App;
  3. using Android.Content.PM;
  4. using Android.Runtime;
  5. using Android.Views;
  6. using Android.Widget;
  7. using Android.OS;
  8. namespace Permission.Droid
  9. {
  10. [Activity(Label = "Permission", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
  11. public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
  12. {
  13. readonly string[] permission =
  14. {
  15. Android.Manifest.Permission.AccessCoarseLocation,
  16. Android.Manifest.Permission.AccessFineLocation,
  17. Android.Manifest.Permission.ReadExternalStorage,
  18. Android.Manifest.Permission.Internet,
  19. Android.Manifest.Permission.Camera,
  20. Android.Manifest.Permission.AnswerPhoneCalls,
  21. Android.Manifest.Permission.Bluetooth
  22. };
  23. const int RequestId = 0;
  24. protected override void OnCreate(Bundle bundle)
  25. {
  26. TabLayoutResource = Resource.Layout.Tabbar;
  27. ToolbarResource = Resource.Layout.Toolbar;
  28. base.OnCreate(bundle);
  29. Xamarin.Essentials.Platform.Init(this, bundle);
  30. RequestPermissions(permission, RequestId);
  31. global::Xamarin.Forms.Forms.Init(this, bundle);
  32. LoadApplication(new App());
  33. }
  34. public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
  35. {
  36. Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  37. base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  38. }
  39. }
  40. }
Step 8
Next, select the build & deploy option, followed by selecting from the list of Android Emulators. You can choose any API (Application Program Interface) to run it.
Android Application Aunthetications Using Xamarin.Forms
Output
After a few seconds, you will see your app working.
Android Application Aunthetications Using Xamarin.Forms
Device Location
Android Application Aunthetications Using Xamarin.Forms
Media Device
Android Application Aunthetications Using Xamarin.Forms
Camera
Android Application Aunthetications Using Xamarin.Forms
Access Phone Calls
Android Application Aunthetications Using Xamarin.Forms
Finally, we have successfully created Xamarin.Forms

Conclusion

I hope you learned about Android Application Authentication using Xamarin.Forms with C#.