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.
Step 2
Install the following NuGet package to your project.
Update all existing packages.
And install the new packages.
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.
Next, a dialogue box will open. Choose image location and add images.
Step 4
The Imae is added successfully for Android.
Step 5
Now, Open Solution Explorer >> Project Name >> MainPage.xaml. Open the design view of this page.
The code is given below.
XAML Code
- <?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:Permission"
- x:Class="Permission.MainPage">
-
- <StackLayout>
- <Image Source="download.png"/>
- </StackLayout>
-
- </ContentPage>
Step 6
Next, go to ProjectName.Android >> properties.
Open the design page.
In the MainActiviy.cs page in required permissions clicks on Android Manifest to click on the checkbox.
Choose Android Manifest and for the required permission select on "LOCATION, CAMERA, CONTACTS, MEDIA FILES" and etc..,
Step 7
Now, Open Solution Explorer >> Project Name.Android >> MainActivity.csl. Open the design view of this page.
The code is given below.
C# Code
- using System;
-
- using Android.App;
- using Android.Content.PM;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.OS;
-
- namespace Permission.Droid
- {
- [Activity(Label = "Permission", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
- public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
- {
- readonly string[] permission =
- {
- Android.Manifest.Permission.AccessCoarseLocation,
- Android.Manifest.Permission.AccessFineLocation,
- Android.Manifest.Permission.ReadExternalStorage,
- Android.Manifest.Permission.Internet,
- Android.Manifest.Permission.Camera,
- Android.Manifest.Permission.AnswerPhoneCalls,
- Android.Manifest.Permission.Bluetooth
-
- };
-
- const int RequestId = 0;
-
- protected override void OnCreate(Bundle bundle)
- {
- TabLayoutResource = Resource.Layout.Tabbar;
- ToolbarResource = Resource.Layout.Toolbar;
-
- base.OnCreate(bundle);
- Xamarin.Essentials.Platform.Init(this, bundle);
- RequestPermissions(permission, RequestId);
- global::Xamarin.Forms.Forms.Init(this, bundle);
- LoadApplication(new App());
-
- }
-
- public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
- {
- Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
-
- base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- }
- }
- }
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.
Output
After a few seconds, you will see your app working.
Device Location
Media Device
Camera
Access Phone Calls
Finally, we have successfully created Xamarin.Forms
Conclusion
I hope you learned about Android Application Authentication using Xamarin.Forms with C#.