Android Application Aunthetications Using Xamarin.Forms

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.     
  7.     <StackLayout>    
  8.         <Image Source="download.png"/>    
  9.     </StackLayout>    
  10.     
  11. </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.     
  3. using Android.App;    
  4. using Android.Content.PM;    
  5. using Android.Runtime;    
  6. using Android.Views;    
  7. using Android.Widget;    
  8. using Android.OS;    
  9.     
  10. namespace Permission.Droid    
  11. {    
  12.     [Activity(Label = "Permission", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]    
  13.     public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity    
  14.     {    
  15.         readonly string[] permission =    
  16.        {    
  17.             Android.Manifest.Permission.AccessCoarseLocation,    
  18.             Android.Manifest.Permission.AccessFineLocation,    
  19.             Android.Manifest.Permission.ReadExternalStorage,    
  20.             Android.Manifest.Permission.Internet,    
  21.             Android.Manifest.Permission.Camera,    
  22.             Android.Manifest.Permission.AnswerPhoneCalls,    
  23.             Android.Manifest.Permission.Bluetooth    
  24.     
  25.         };    
  26.     
  27.         const int RequestId = 0;    
  28.     
  29.         protected override void OnCreate(Bundle bundle)    
  30.         {    
  31.             TabLayoutResource = Resource.Layout.Tabbar;    
  32.             ToolbarResource = Resource.Layout.Toolbar;    
  33.     
  34.             base.OnCreate(bundle);    
  35.             Xamarin.Essentials.Platform.Init(this, bundle);    
  36.             RequestPermissions(permission, RequestId);    
  37.             global::Xamarin.Forms.Forms.Init(this, bundle);    
  38.             LoadApplication(new App());    
  39.     
  40.         }    
  41.     
  42.         public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)    
  43.         {    
  44.             Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);    
  45.     
  46.             base.OnRequestPermissionsResult(requestCode, permissions, grantResults);    
  47.         }    
  48.     }    
  49. }    
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#.


Similar Articles