Add Email Authentication To Xamarin.Android App Using Firebase - Part Two

First, register for Firebase app and get App ID and API Key. Refer to part one for the same.
Step 1
 
Add a new XML file, go to Solution Explorer-> Project Name-> Resources-> Values. Right-click to add a new item, select XML, and give it a name as styles.axml. Open this XML file and add the following code.
 
(Folder Name: values, File Name: styles.xml)
 
XML Code
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <resources>  
  3.   <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">  
  4.     <item name="colorPrimary">@color/colorPrimary</item>  
  5.     <item name="colorPrimaryDark">@color/colorPrimaryDark</item>  
  6.     <item name="colorAccent">@color/colorAccent</item>  
  7.   </style>  
  8. </resources>  
Step 2
 
Add another XML file. For that, go to Solution Explorer-> Project Name-> Resources-> Values. Right-click to add a new item, select XML, and give it a name as colors.axml. Open this XML file and add the following code.
 
(Folder Name: values, File Name: colors.xml)
 
XML Code
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <resources>  
  3.    <color name="colorPrimary">#607D8B</color>    
  4.    <color name="colorPrimaryDark">#37474F</color>  
  5.    <color name="colorAccent">#ECEFF1</color>  
  6. </resources>  
Step 3 - Main Layout
 
Open Solution Explorer-> Project Name-> Resources-> Layout-> Main.axml. Open this main layout file and add the following code.
 
(File Name: Main.axml , Folder Name: Layout)
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/activity_main"  
  4.     android:padding="16dp"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent">  
  7.     <ImageView  
  8.         android:id="@+id/icon"  
  9.         android:layout_alignParentTop="true"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:background="@drawable/firebase"  
  12.         android:layout_width="100dp"  
  13.         android:layout_height="100dp" />  
  14.     <android.support.design.widget.TextInputLayout  
  15.         android:layout_below="@+id/icon"  
  16.         android:id="@+id/login_input_email"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content">  
  19.         <EditText  
  20.             android:hint="Enter your Email"  
  21.             android:id="@+id/login_email"  
  22.             android:inputType="textCapWords"  
  23.             android:maxLines="1"  
  24.             android:layout_width="match_parent"  
  25.             android:layout_height="wrap_content" />  
  26.     </android.support.design.widget.TextInputLayout>  
  27.     <android.support.design.widget.TextInputLayout  
  28.         android:layout_below="@+id/login_input_email"  
  29.         android:id="@+id/login_input_password"  
  30.         android:layout_width="match_parent"  
  31.         android:layout_height="wrap_content">  
  32.         <EditText  
  33.             android:hint="Enter your Password"  
  34.             android:id="@+id/login_password"  
  35.             android:inputType="textPassword"  
  36.             android:maxLines="1"  
  37.             android:layout_width="match_parent"  
  38.             android:layout_height="wrap_content" />  
  39.     </android.support.design.widget.TextInputLayout>  
  40.     <Button  
  41.         android:layout_below="@+id/login_input_password"  
  42.         android:id="@+id/login_btn_login"  
  43.         android:text="Login"  
  44.         android:background="#263238"  
  45.         android:layout_width="match_parent"  
  46.         android:layout_height="wrap_content"  
  47.         style="@style/Widget.AppCompat.Button.Colored" />  
  48.     <TextView  
  49.         android:layout_below="@+id/login_btn_login"  
  50.         android:id="@+id/login_btn_forget_password"  
  51.         android:text="Forget Password"  
  52.         android:textStyle="bold"  
  53.         android:clickable="true"  
  54.         android:layout_centerHorizontal="true"  
  55.         android:layout_width="wrap_content"  
  56.         android:layout_height="wrap_content"  
  57.         android:textColor="@color/colorPrimaryDark"  
  58.         style="@style/Widget.AppCompat.Button.Borderless" />  
  59.     <LinearLayout  
  60.         android:layout_below="@+id/login_btn_forget_password"  
  61.         android:id="@+id/login_layout_or"  
  62.         android:gravity="center"  
  63.         android:orientation="horizontal"  
  64.         android:layout_width="match_parent"  
  65.         android:layout_height="wrap_content">  
  66.         <View  
  67.             android:layout_width="200dp"  
  68.             android:layout_height="1dp"  
  69.             android:background="#C4C8C9"  
  70.             android:layout_margin="5dp" />  
  71.         <TextView  
  72.             android:padding="5dp"  
  73.             android:text="OR"  
  74.             android:layout_width="wrap_content"  
  75.             android:layout_height="wrap_content" />  
  76.         <View  
  77.             android:layout_width="200dp"  
  78.             android:layout_height="1dp"  
  79.             android:background="#C4C8C9"  
  80.             android:layout_margin="5dp" />  
  81.     </LinearLayout>  
  82.     <TextView  
  83.         android:layout_below="@+id/login_layout_or"  
  84.         android:id="@+id/login_btn_sign_up"  
  85.         android:text="Sign Up"  
  86.         android:textStyle="bold"  
  87.         android:clickable="true"  
  88.         android:layout_centerHorizontal="true"  
  89.         android:layout_width="wrap_content"  
  90.         android:layout_height="wrap_content"  
  91.         style="@style/Widget.AppCompat.Button.Borderless"  
  92.         android:textColor="@color/colorPrimaryDark" />  
  93. </RelativeLayout>  
Step 4 - Main Activity Class
 
NoteMust change your App ID and API key in the main activity class before building the project. 
 
Now, go to Solution Explorer-> Project Name-> MainActivity and add the following code to main activity with appropriate namespaces.
 
(FileName: MainActivity)
 
C# Code
  1. using Android.App;  
  2. using Android.Widget;  
  3. using Android.OS;  
  4. using Firebase;  
  5. using Firebase.Auth;  
  6. using System;  
  7. using static Android.Views.View;  
  8. using Android.Views;  
  9. using Android.Gms.Tasks;  
  10. using Android.Support.Design.Widget;  
  11. namespace XamarinFirebaseAuth  
  12. {  
  13.     [Activity(Label = "XamarinFirebaseAuth", MainLauncher = true, Theme ="@style/AppTheme")]  
  14.     public class MainActivity : Activity, IOnClickListener, IOnCompleteListener  
  15.     {  
  16.         Button btnLogin;  
  17.         EditText input_email, input_password;  
  18.         TextView btnSignUp, btnForgetPassword;  
  19.         RelativeLayout activity_main;  
  20.         public static FirebaseApp app;  
  21.         FirebaseAuth auth;  
  22.         protected override void OnCreate(Bundle savedInstanceState)  
  23.         {  
  24.             base.OnCreate(savedInstanceState);  
  25.             // Set our view from the "main" layout resource  
  26.             SetContentView(Resource.Layout.Main);  
  27.             //Init Auth  
  28.             InitFirebaseAuth();  
  29.             //Views  
  30.             btnLogin = FindViewById<Button>(Resource.Id.login_btn_login);  
  31.             input_email = FindViewById<EditText>(Resource.Id.login_email);  
  32.             input_password = FindViewById<EditText>(Resource.Id.login_password);  
  33.             btnSignUp = FindViewById<TextView>(Resource.Id.login_btn_sign_up);  
  34.             btnForgetPassword = FindViewById<TextView>(Resource.Id.login_btn_forget_password);  
  35.             activity_main = FindViewById<RelativeLayout>(Resource.Id.activity_main);  
  36.             btnSignUp.SetOnClickListener(this);  
  37.             btnLogin.SetOnClickListener(this);  
  38.             btnForgetPassword.SetOnClickListener(this);  
  39.         }  
  40.         private void InitFirebaseAuth()  
  41.         {  
  42.             var options = new FirebaseOptions.Builder()  
  43.                .SetApplicationId("AIzaSyDDkjTIE-LQMNCfPOwzR8kX0-IPENxl_xY")  
  44.                .SetApiKey("AIzaSyBmuAwrNEgENM40rnjUToHHMraFXQOyuPE")  
  45.                .Build();  
  46.             if (app == null)  
  47.                 app = FirebaseApp.InitializeApp(this, options);  
  48.             auth = FirebaseAuth.GetInstance(app);  
  49.         }  
  50.         public void OnClick(View v)  
  51.         {  
  52.             if(v.Id == Resource.Id.login_btn_forget_password)  
  53.             {  
  54.                 StartActivity(new Android.Content.Intent(thistypeof(ForgetPassword)));  
  55.                 Finish();  
  56.             }  
  57.             else  
  58.             if(v.Id == Resource.Id.login_btn_sign_up)  
  59.             {  
  60.                 StartActivity(new Android.Content.Intent(thistypeof(SignUp)));  
  61.                 Finish();  
  62.             }  
  63.             else  
  64.             if (v.Id == Resource.Id.login_btn_login)  
  65.             {  
  66.                 LoginUser(input_email.Text, input_password.Text);  
  67.             }  
  68.         }  
  69.         private void LoginUser(string email ,string password)  
  70.         {  
  71.             auth.SignInWithEmailAndPassword(email, password).AddOnCompleteListener(this);  
  72.         }  
  73.         public void OnComplete(Task task)  
  74.         {  
  75.             if (task.IsSuccessful)  
  76.             {  
  77.                 StartActivity(new Android.Content.Intent(thistypeof(DashBoard)));  
  78.                 Finish();  
  79.             }  
  80.             else  
  81.             {  
  82.                 Snackbar snackbar = Snackbar.Make(activity_main, "Login Failed ", Snackbar.LengthShort);  
  83.                 snackbar.Show();  
  84.             }  
  85.         }  
  86.     }  
  87.   }  
Step 5 - Add Layout For SignUp
 
Next, add a new Layout by going to Solution Explorer-> Project Name-> Resources-> Layout. Right-click to add a new item, select Layout, and give it a name as SignUp.axml. Open this layout file and add the following code.
 
(Folder Name: Layout , File Name: SignUp.axml)
 
XML Code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/activity_sign_up"  
  4.     android:padding="16dp"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent">  
  7.     <ImageView  
  8.         android:id="@+id/icon"  
  9.         android:layout_alignParentTop="true"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:background="@drawable/firebase"  
  12.         android:layout_width="100dp"  
  13.         android:layout_height="100dp" />  
  14.     <android.support.design.widget.TextInputLayout  
  15.         android:layout_below="@+id/icon"  
  16.         android:id="@+id/signup_input_email"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content">  
  19.         <EditText  
  20.             android:hint="Enter your Email"  
  21.             android:id="@+id/signup_email"  
  22.             android:inputType="textCapWords"  
  23.             android:maxLines="1"  
  24.             android:layout_width="match_parent"  
  25.             android:layout_height="wrap_content" />  
  26.     </android.support.design.widget.TextInputLayout>  
  27.     <android.support.design.widget.TextInputLayout  
  28.         android:layout_below="@+id/signup_input_email"  
  29.         android:id="@+id/signup_input_password"  
  30.         android:layout_width="match_parent"  
  31.         android:layout_height="wrap_content">  
  32.         <EditText  
  33.             android:hint="Enter your Password"  
  34.             android:id="@+id/signup_password"  
  35.             android:inputType="textPassword"  
  36.             android:maxLines="1"  
  37.             android:layout_width="match_parent"  
  38.             android:layout_height="wrap_content" />  
  39.     </android.support.design.widget.TextInputLayout>  
  40.     <Button  
  41.         android:layout_below="@+id/signup_input_password"  
  42.         android:id="@+id/signup_btn_register"  
  43.         android:text="Register"  
  44.         android:background="#263238"  
  45.         android:layout_width="match_parent"  
  46.         android:layout_height="wrap_content"  
  47.         style="@style/Widget.AppCompat.Button.Colored" />  
  48.     <TextView  
  49.         android:layout_below="@+id/signup_btn_register"  
  50.         android:id="@+id/signup_btn_forget_password"  
  51.         android:text="Forget Password"  
  52.         android:textStyle="bold"  
  53.         android:clickable="true"  
  54.         android:layout_centerHorizontal="true"  
  55.         android:layout_width="wrap_content"  
  56.         android:layout_height="wrap_content"  
  57.         android:textColor="@color/colorPrimaryDark"  
  58.         style="@style/Widget.AppCompat.Button.Borderless" />  
  59.     <LinearLayout  
  60.         android:layout_below="@+id/signup_btn_forget_password"  
  61.         android:id="@+id/signup_layout_or"  
  62.         android:gravity="center"  
  63.         android:orientation="horizontal"  
  64.         android:layout_width="match_parent"  
  65.         android:layout_height="wrap_content">  
  66.         <View  
  67.             android:layout_width="200dp"  
  68.             android:layout_height="1dp"  
  69.             android:background="#C4C8C9"  
  70.             android:layout_margin="5dp" />  
  71.         <TextView  
  72.             android:padding="5dp"  
  73.             android:text="OR"  
  74.             android:layout_width="wrap_content"  
  75.             android:layout_height="wrap_content" />  
  76.         <View  
  77.             android:layout_width="200dp"  
  78.             android:layout_height="1dp"  
  79.             android:background="#C4C8C9"  
  80.             android:layout_margin="5dp" />  
  81.     </LinearLayout>  
  82.     <TextView  
  83.         android:layout_below="@+id/signup_layout_or"  
  84.         android:id="@+id/signup_btn_login"  
  85.         android:text="Already Account ? Login Me"  
  86.         android:textStyle="bold"  
  87.         android:clickable="true"  
  88.         android:layout_centerHorizontal="true"  
  89.         android:layout_width="wrap_content"  
  90.         android:layout_height="wrap_content"  
  91.         style="@style/Widget.AppCompat.Button.Borderless"  
  92.         android:textColor="@color/colorPrimaryDark" />  
  93. </RelativeLayout>  
Step 6 - Create SignUp Activity
 
Add a new Activity. For this, open Solution Explorer-> Project Name-> right click to add a new item and select Activity. Give it a name like SignUp and add the following code putting appropriate namespaces.
 
(FileName: SignUp)
 
C# Code
  1. using Android.App;  
  2. using Android.Content;  
  3. using Android.Gms.Tasks;  
  4. using Android.OS;  
  5. using Android.Support.Design.Widget;  
  6. using Android.Views;  
  7. using Android.Widget;  
  8. using Firebase.Auth;  
  9. using static Android.Views.View;  
  10. namespace XamarinFirebaseAuth  
  11. {  
  12.     [Activity(Label = "SignUp", Theme ="@style/AppTheme")]  
  13.     public class SignUp : Activity, IOnClickListener, IOnCompleteListener  
  14.     {  
  15.         Button btnSignup;  
  16.         TextView btnLogin, btnForgetPass;  
  17.         EditText input_email, input_password;  
  18.         RelativeLayout activity_sign_up;  
  19.         FirebaseAuth auth;  
  20.         public void OnClick(View v)  
  21.         {  
  22.             if(v.Id == Resource.Id.signup_btn_login)  
  23.             {  
  24.                 StartActivity(new Intent(thistypeof(MainActivity)));  
  25.                 Finish();  
  26.             }  
  27.             else  
  28.             if (v.Id == Resource.Id.signup_btn_forget_password)  
  29.             {  
  30.                 StartActivity(new Intent(thistypeof(ForgetPassword)));  
  31.                 Finish();  
  32.             }  
  33.             else  
  34.             if (v.Id == Resource.Id.signup_btn_register)  
  35.             {  
  36.                 SignUpUser(input_email.Text, input_password.Text);   
  37.             }  
  38.         }  
  39.         private void SignUpUser(string email, string password)  
  40.         {  
  41.             auth.CreateUserWithEmailAndPassword(email, password).AddOnCompleteListener(thisthis);  
  42.         }  
  43.         protected override void OnCreate(Bundle savedInstanceState)  
  44.         {  
  45.             base.OnCreate(savedInstanceState);  
  46.             // Create your application here  
  47.             SetContentView(Resource.Layout.SignUp);  
  48.             //Init Firebase  
  49.             auth = FirebaseAuth.GetInstance(MainActivity.app);  
  50.             //Views  
  51.             btnSignup = FindViewById<Button>(Resource.Id.signup_btn_register);  
  52.             btnLogin = FindViewById<TextView>(Resource.Id.signup_btn_login);  
  53.             btnForgetPass = FindViewById<TextView>(Resource.Id.signup_btn_forget_password);  
  54.             input_email = FindViewById<EditText>(Resource.Id.signup_email);  
  55.             input_password = FindViewById<EditText>(Resource.Id.signup_password);  
  56.             activity_sign_up = FindViewById<RelativeLayout>(Resource.Id.activity_sign_up);  
  57.             btnLogin.SetOnClickListener(this);  
  58.             btnSignup.SetOnClickListener(this);  
  59.             btnForgetPass.SetOnClickListener(this);  
  60.         }  
  61.         public void OnComplete(Task task)  
  62.         {  
  63.             if(task.IsSuccessful == true)  
  64.             {  
  65.                 Snackbar snackbar = Snackbar.Make(activity_sign_up, "Register Successfully ", Snackbar.LengthShort);  
  66.                 snackbar.Show();  
  67.             }  
  68.             else  
  69.             {  
  70.                 Snackbar snackbar = Snackbar.Make(activity_sign_up, "Register Failed ", Snackbar.LengthShort);  
  71.                 snackbar.Show();  
  72.             }  
  73.         }  
  74.     }  
  75. }  
Step 7 - Add Layout For DashBoard
 
Again, add a new Layout named DashBoard.axml and add the following code.
 
(FileName: DashBoard.axml)

XML Code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/activity_dashboard"  
  4.     android:padding="16dp"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent">  
  7.     <ImageView  
  8.         android:id="@+id/icon"  
  9.         android:layout_alignParentTop="true"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:background="@drawable/firebase"  
  12.         android:layout_width="100dp"  
  13.         android:layout_height="100dp" />  
  14.     <TextView  
  15.         android:layout_below="@+id/icon"  
  16.         android:id="@+id/dashboard_welcome"  
  17.         android:text="Welcome, user"  
  18.         android:layout_centerHorizontal="true"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         style="@style/Widget.AppCompat.Button.Borderless"  
  22.         android:textColor="@color/colorPrimaryDark" />  
  23.     <android.support.design.widget.TextInputLayout  
  24.         android:layout_below="@+id/dashboard_welcome"  
  25.         android:id="@+id/dashboard_input_newpassword"  
  26.         android:layout_width="match_parent"  
  27.         android:layout_height="wrap_content">  
  28.         <EditText  
  29.             android:hint="Enter new Password"  
  30.             android:id="@+id/dashboard_newpassword"  
  31.             android:inputType="textPassword"  
  32.             android:maxLines="1"  
  33.             android:layout_width="match_parent"  
  34.             android:layout_height="wrap_content" />  
  35.     </android.support.design.widget.TextInputLayout>  
  36.     <Button  
  37.         android:layout_below="@+id/dashboard_input_newpassword"  
  38.         android:id="@+id/dashboard_btn_change_pass"  
  39.         android:text="Change Password"  
  40.         android:background="#263238"  
  41.         android:layout_width="match_parent"  
  42.         android:layout_height="wrap_content" />  
  43.     <Button  
  44.         android:layout_marginTop="20dp"  
  45.         android:layout_below="@+id/dashboard_btn_change_pass"  
  46.         android:id="@+id/dashboard_btn_logout"  
  47.         android:text="Logout"  
  48.         android:background="#263238"  
  49.         android:layout_width="match_parent"  
  50.         android:layout_height="wrap_content" />  
  51. </RelativeLayout>  
Step 8 - Create Dashboard Activity
 
Again, add another Activity named Dashboard and add the following code with appropriate namespaces.
 
(FileName: Dashboard.cs)
 
C# Code
  1. using Android.App;  
  2. using Android.Content;  
  3. using Android.Gms.Tasks;  
  4. using Android.OS;  
  5. using Android.Support.Design.Widget;  
  6. using Android.Support.V7.App;  
  7. using Android.Views;  
  8. using Android.Widget;  
  9. using Firebase.Auth;  
  10. using static Android.Views.View;  
  11. namespace XamarinFirebaseAuth  
  12. {  
  13.     [Activity(Label = "DashBoard" ,Theme = "@style/AppTheme")]  
  14.     public class DashBoard : AppCompatActivity, IOnClickListener, IOnCompleteListener  
  15.     {  
  16.         TextView txtWelcome;  
  17.         EditText input_new_password;  
  18.         Button btnChangePass, btnLogout;  
  19.         RelativeLayout activity_dashboard;  
  20.         FirebaseAuth auth;  
  21.         public void OnClick(View v)  
  22.         {  
  23.             if (v.Id == Resource.Id.dashboard_btn_change_pass)  
  24.                 ChangePassword(input_new_password.Text);  
  25.             else if (v.Id == Resource.Id.dashboard_btn_logout)  
  26.                 LogoutUser();  
  27.         }  
  28.         private void LogoutUser()  
  29.         {  
  30.             auth.SignOut();  
  31.             if(auth.CurrentUser == null)  
  32.             {  
  33.                 StartActivity(new Intent(thistypeof(MainActivity)));  
  34.                 Finish();  
  35.             }  
  36.         }  
  37.         private void ChangePassword(string newPassword)  
  38.         {  
  39.             FirebaseUser user = auth.CurrentUser;  
  40.             user.UpdatePassword(newPassword)  
  41.             .AddOnCompleteListener(this);  
  42.         }  
  43.         protected override void OnCreate(Bundle savedInstanceState)  
  44.         {  
  45.             base.OnCreate(savedInstanceState);  
  46.             SetContentView(Resource.Layout.DashBoard);  
  47.             //Init Firebase  
  48.             auth = FirebaseAuth.GetInstance(MainActivity.app);  
  49.             //View  
  50.             btnChangePass = FindViewById<Button>(Resource.Id.dashboard_btn_change_pass);  
  51.             txtWelcome = FindViewById<TextView>(Resource.Id.dashboard_welcome);  
  52.             btnLogout = FindViewById<Button>(Resource.Id.dashboard_btn_logout);  
  53.             input_new_password = FindViewById<EditText>(Resource.Id.dashboard_newpassword);  
  54.             activity_dashboard = FindViewById<RelativeLayout>(Resource.Id.activity_dashboard);  
  55.             btnChangePass.SetOnClickListener(this);  
  56.             btnLogout.SetOnClickListener(this);  
  57.             //Check Session  
  58.             if (auth != null)  
  59.                 txtWelcome.Text = "Welcome , " + auth.CurrentUser.Email;  
  60.         }  
  61.         public void OnComplete(Task task)  
  62.         {  
  63.             if (task.IsSuccessful == true)  
  64.             {  
  65.                 Snackbar snackbar = Snackbar.Make(activity_dashboard, "Password has been Changed!", Snackbar.LengthShort);  
  66.                 snackbar.Show();  
  67.             }  
  68.         }  
  69.     }  
  70. }  
Step 9 - Add Layout For ForgetPassword
 
Similarly, add a new Layout - ForgetPassword.axml and add the following code.
 
(FileName: ForgetPassword.axml)
 
XML Code 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/activity_forget"  
  4.     android:padding="16dp"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent">  
  7.     <ImageView  
  8.         android:id="@+id/icon"  
  9.         android:layout_alignParentTop="true"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:background="@drawable/firebase"  
  12.         android:layout_width="100dp"  
  13.         android:layout_height="100dp" />  
  14.     <TextView  
  15.         android:layout_below="@+id/icon"  
  16.         android:id="@+id/forget_message"  
  17.         android:text="We just need your registered email to send your password reset instructions"  
  18.         android:layout_centerHorizontal="true"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         style="@style/Widget.AppCompat.Button.Borderless"  
  22.         android:textColor="@color/colorPrimaryDark" />  
  23.     <android.support.design.widget.TextInputLayout  
  24.         android:layout_below="@+id/forget_message"  
  25.         android:id="@+id/forget_input_email"  
  26.         android:layout_width="match_parent"  
  27.         android:layout_height="wrap_content">  
  28.         <EditText  
  29.             android:hint="Enter your email"  
  30.             android:id="@+id/forget_email"  
  31.             android:inputType="textCapWords"  
  32.             android:maxLines="1"  
  33.             android:layout_width="match_parent"  
  34.             android:layout_height="wrap_content" />  
  35.     </android.support.design.widget.TextInputLayout>  
  36.     <Button  
  37.         android:layout_below="@+id/forget_input_email"  
  38.         android:id="@+id/forget_btn_reset"  
  39.         android:text="Reset Password"  
  40.         android:background="#263238"  
  41.         android:layout_width="match_parent"  
  42.         android:layout_height="wrap_content"  
  43.         style="@style/Widget.AppCompat.Button.Colored" />  
  44.     <TextView  
  45.         android:layout_below="@+id/forget_btn_reset"  
  46.         android:id="@+id/forget_btn_back"  
  47.         android:text="Back"  
  48.         android:layout_centerHorizontal="true"  
  49.         android:layout_width="wrap_content"  
  50.         android:layout_height="wrap_content"  
  51.         style="@style/Widget.AppCompat.Button.Borderless"  
  52.         android:textColor="@color/colorPrimaryDark" />  
  53. </RelativeLayout>  
Step 10 - Create ForgetPassword Activity
 
Lastly, add one more Activity named ForgetPassword and add the following code with appropriate namespaces.
 
(FileName: ForgetPassword.cs)
 
C# Code
  1. using Android.App;  
  2. using Android.Content;  
  3. using Android.Gms.Tasks;  
  4. using Android.OS;  
  5. using Android.Support.Design.Widget;  
  6. using Android.Support.V7.App;  
  7. using Android.Views;  
  8. using Android.Widget;  
  9. using Firebase.Auth;  
  10. using static Android.Views.View;  
  11. namespace XamarinFirebaseAuth  
  12. {  
  13.     [Activity(Label = "ForgetPasswordcs", Theme ="@style/AppTheme")]  
  14.     public class ForgetPassword : AppCompatActivity, IOnClickListener, IOnCompleteListener  
  15.     {  
  16.         EditText input_email;  
  17.         Button btnResetPas;  
  18.         TextView btnBack;  
  19.         RelativeLayout activity_forget;  
  20.         FirebaseAuth auth;  
  21.         public void OnClick(View v)  
  22.         {  
  23.             if (v.Id == Resource.Id.forget_btn_back)  
  24.             {  
  25.                 StartActivity(new Intent(thistypeof(MainActivity)));  
  26.                 Finish();  
  27.             }  
  28.             else if(v.Id == Resource.Id.forget_btn_reset)  
  29.             {  
  30.                 ResetPassword(input_email.Text);  
  31.             }  
  32.         }  
  33.         private void ResetPassword(string email)  
  34.         {  
  35.             auth.SendPasswordResetEmail(email).AddOnCompleteListener(thisthis);  
  36.         }  
  37.         protected override void OnCreate(Bundle savedInstanceState)  
  38.         {  
  39.             base.OnCreate(savedInstanceState);  
  40.             SetContentView(Resource.Layout.ForgetPassword);  
  41.             //Init Firebase  
  42.             auth = FirebaseAuth.GetInstance(MainActivity.app);  
  43.             //Views  
  44.             input_email = FindViewById<EditText>(Resource.Id.forget_email);  
  45.             btnResetPas = FindViewById<Button>(Resource.Id.forget_btn_reset);  
  46.             btnBack = FindViewById<TextView>(Resource.Id.forget_btn_back);  
  47.             activity_forget = FindViewById<RelativeLayout>(Resource.Id.activity_forget);  
  48.             btnResetPas.SetOnClickListener(this);  
  49.             btnBack.SetOnClickListener(this);  
  50.         }  
  51.         public void OnComplete(Task task)  
  52.         {  
  53.             if (task.IsSuccessful == false)  
  54.             {  
  55.                 Snackbar snackbar = Snackbar.Make(activity_forget, "Reset Password Failed!", Snackbar.LengthShort);  
  56.                 snackbar.Show();  
  57.             }else  
  58.             {  
  59.                 Snackbar snackbar = Snackbar.Make(activity_forget, "Reset Password link send to email : " + input_email.Text, Snackbar.LengthShort);  
  60.                 snackbar.Show();  
  61.             }  
  62.         }  
  63.     }  
  64. }  
Finally, we are done with our Xamarin Firebase Auth app. Just rebuild and run the project. You will have results like below.
 
Register User
 
 
 
SignIn User
 
 
 
Password Reset 
 
 
 
Summary
 
This was the process of creating a Xamarin Firebase Auth App in Xamarin.Android, using Visual Studio. Please share your comments and feedback.


Similar Articles