Simple Login and Registration using SQLite Database

Introduction 
 
This blog demonstrates how to create a Login and Registration form and then save the form details in SQLite database in Xamarin Android. Finally, the post shows how to allow the user to log in once registered. 
 
Steps 
 
First, create new a project in Xamarin Android in Visual Studio (Ctrl+Shift+O). Select Android templates and select Android App (Xamarin)  and Single View app template (it depends on our requirement). 
 
Below are the steps we will perform:
 
  1. Create a UI design for 3 pages - SignUp.axml (for User Registration), Main.axml (for Login User) & Welcome.axml (for screen after login).
  2. UserDetails.cs (Strongly Typed DTO)
  3. Helper.cs (Helper Class - for DB Operation)
  4. Activities w.r.t. UI Pages - SignUp.cs (for User Registration activity), MainActivity.cs (for User Login activity) & Welcome.cs (for welcome screen activity after login).  
UI Design
 
Create a SignUp (Registration) page.Go to Resources/Layout folder and create new Layout file (SignUp.axml) file. Once it is created, update it with the following code.
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:orientation="vertical"    
  4.     android:layout_width="match_parent"    
  5.     android:layout_height="match_parent"    
  6.     android:minWidth="25px"    
  7.     android:minHeight="25px">    
  8.         
  9.     <EditText    
  10.         android:hint="Username"    
  11.         android:layout_width="match_parent"    
  12.         android:layout_height="wrap_content"    
  13.         android:layout_marginLeft="20dp"    
  14.         android:layout_marginRight="20dp"    
  15.         android:id="@+id/edtusername"/>    
  16.     <EditText    
  17.         android:hint="Email"    
  18.         android:layout_width="match_parent"    
  19.         android:layout_height="wrap_content"    
  20.         android:layout_marginLeft="20dp"    
  21.         android:layout_marginRight="20dp"    
  22.         android:id="@+id/edtEmail"    
  23.          />    
  24.        
  25.     <EditText    
  26.         android:hint="Password"    
  27.         android:layout_width="match_parent"    
  28.         android:layout_height="wrap_content"    
  29.         android:layout_marginLeft="20dp"    
  30.         android:layout_marginRight="20dp"    
  31.         android:id="@+id/edtpassword"    
  32.         android:inputType="textPassword" />    
  33.     <EditText    
  34.         android:hint="First Name"    
  35.         android:layout_width="match_parent"    
  36.         android:layout_height="wrap_content"    
  37.         android:layout_marginLeft="20dp"    
  38.         android:layout_marginRight="20dp"    
  39.         android:id="@+id/edtfname"/>    
  40.     <EditText    
  41.         android:hint="Last Name"    
  42.         android:layout_width="match_parent"    
  43.         android:layout_height="wrap_content"    
  44.         android:layout_marginLeft="20dp"    
  45.         android:layout_marginRight="20dp"    
  46.         android:id="@+id/edtlname"/>    
  47.      <EditText    
  48.         android:hint="Mobile"    
  49.         android:layout_width="match_parent"    
  50.         android:layout_height="wrap_content"    
  51.         android:layout_marginLeft="20dp"    
  52.         android:layout_marginRight="20dp"    
  53.         android:id="@+id/edtMobile"    
  54.         android:inputType="phone" />    
  55.     <EditText    
  56.         android:hint="Address"    
  57.         android:layout_width="match_parent"    
  58.         android:layout_height="wrap_content"    
  59.         android:layout_marginLeft="20dp"    
  60.         android:layout_marginRight="20dp"    
  61.         android:id="@+id/edtAddress"/>    
  62.     <EditText    
  63.         android:hint="Country"    
  64.         android:layout_width="match_parent"    
  65.         android:layout_height="wrap_content"    
  66.         android:layout_marginLeft="20dp"    
  67.         android:layout_marginRight="20dp"    
  68.         android:id="@+id/edtCountry"/>    
  69.     <Button    
  70.         android:text="Create"    
  71.         android:layout_width="match_parent"    
  72.         android:layout_height="wrap_content"    
  73.         android:layout_marginLeft="20dp"    
  74.         android:layout_marginRight="20dp"    
  75.         android:id="@+id/btnCreate" />    
  76.     <Button    
  77.         android:text="Back to Home"    
  78.         android:layout_width="match_parent"    
  79.         android:layout_height="wrap_content"    
  80.         android:layout_marginLeft="20dp"    
  81.         android:layout_marginRight="20dp"    
  82.         android:id="@+id/btnBack" />    
  83. </LinearLayout>  
Similarly, create new Layout file (axml) file for Login.
 
Login (Main.axml)
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:orientation="vertical"    
  4.     android:layout_width="match_parent"    
  5.     android:layout_height="match_parent"    
  6.     android:minWidth="25px"    
  7.     android:minHeight="25px">    
  8.     <EditText    
  9.         android:hint="Username"    
  10.         android:layout_width="match_parent"    
  11.         android:layout_height="wrap_content"    
  12.         android:layout_marginLeft="20dp"    
  13.         android:layout_marginRight="20dp"    
  14.         android:id="@+id/txtusername"/>    
  15.     <EditText    
  16.         android:hint="Password"    
  17.         android:layout_width="match_parent"    
  18.         android:layout_height="wrap_content"    
  19.         android:layout_marginLeft="20dp"    
  20.         android:layout_marginRight="20dp"    
  21.         android:id="@+id/txtpassword"    
  22.         android:inputType="textPassword" />    
  23.     <Button    
  24.         android:text="Sign In"    
  25.         android:layout_width="match_parent"    
  26.         android:layout_height="wrap_content"    
  27.         android:layout_marginLeft="20dp"    
  28.         android:layout_marginRight="20dp"    
  29.         android:id="@+id/btnSign" />    
  30.     <TextView    
  31.         android:text="Or"    
  32.         android:textAppearance="?android:attr/textAppearanceMedium"    
  33.         android:layout_width="match_parent"    
  34.         android:layout_height="wrap_content"    
  35.         android:textColor="#000"    
  36.         android:gravity="center"    
  37.         android:id="@+id/txtOr" />    
  38.     <Button    
  39.         android:text="Register"    
  40.         android:layout_width="match_parent"    
  41.         android:layout_height="wrap_content"    
  42.         android:layout_marginLeft="20dp"    
  43.         android:layout_marginRight="20dp"    
  44.         android:id="@+id/btnSignUp" />    
  45. </LinearLayout>   
Finally, create Welcome page (Welcome.axml)
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:orientation="vertical"    
  4.     android:layout_width="match_parent"    
  5.     android:layout_height="match_parent"    
  6.     android:minWidth="25px"    
  7.     android:minHeight="25px">    
  8.      <TextView    
  9.            
  10.         android:textAppearance="?android:attr/textAppearanceMedium"    
  11.         android:layout_width="match_parent"    
  12.         android:layout_height="wrap_content"    
  13.         android:textColor="#000"    
  14.         android:gravity="left"    
  15.         android:id="@+id/txtWelcome" />    
  16.     <Button    
  17.         android:text="Logout"    
  18.         android:layout_width="match_parent"    
  19.         android:layout_height="wrap_content"    
  20.         android:layout_marginLeft="20dp"    
  21.         android:layout_marginRight="20dp"    
  22.         android:id="@+id/btnLogout" />    
  23. </LinearLayout>   
We have finished the first step of creating UI Design. Now we'll move to second step for creating a DTO class.
 
UserDetails.cs (DTO) 
 
For registration and Log-In, we will require User information, so we will create a strongly-typed class called UserDetails. We'll use the same for storing the details in the SQLite database.
 
Create a new folder Common, add a new empty class file called UserDetails.cs and update the content as shown below.
 
  1. public class UserDetails    
  2.     {    
  3.         public string ID { getset; }    
  4.         public string Username { getset; }    
  5.         public string FirstName { getset; }    
  6.         public string LastName { getset; }    
  7.         public string Address { getset; }    
  8.         public string Country { getset; }    
  9.         public string Email { getset; }    
  10.         public string Password { getset; }    
  11.         public string Mobile { getset; }    
  12.         public UserDetails() { }    
  13.         public UserDetails(string Id, string username, string fname,string lname,string address,string country, string email, string password, string mobile) //Constructor with all parameters    
  14.         {    
  15.             ID = Id;    
  16.             Username = username;    
  17.             FirstName = fname;    
  18.             LastName = lname;    
  19.             Address = address;    
  20.             Country = country;    
  21.             Email = email;    
  22.             Password = password;    
  23.             Mobile = mobile;    
  24.         }    
  25.         public UserDetails(string Password) //Constructor with one parameter    
  26.         {    
  27.             this.Password = Password;    
  28.         }    
  29.            
  30.     }     
 
Helper.cs (Database Helper - SQLite Operations) 
 
We will create a Helper class for DB operation. Let's create a new folder DBHelper, add a new class file called Helper.cs and update it with contents as shown below. It is responsible for handling DBOperation (Create and Read).
 
Note:  You may need to refer below namespaces for SQLite if you encounter reference errors.
            using Android.Database;
            using Android.Database.Sqlite;
  1. public class Helper : SQLiteOpenHelper    
  2.    {    
  3.        private static string _DatabaseName = "clientDatabase";    
  4.     
  5.        public Helper(Context context) : base(context, _DatabaseName, null, 1) { }    
  6.        public override void OnCreate(SQLiteDatabase db)    
  7.        {    
  8.            db.ExecSQL(Helper.CreateQuery);    
  9.        }    
  10.     
  11.        public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)    
  12.        {    
  13.            db.ExecSQL(Helper.DeleteQuery);    
  14.            OnCreate(db);    
  15.        }    
  16.            
  17.        private const string TableName = "adminTable";    
  18.        private const string ColumnID = "id";    
  19.        private const string ColumnUsername = "username";    
  20.        private const string ColumnFirstName = "firstname";    
  21.        private const string ColumnLastName = "lastname";    
  22.        private const string ColumnAddress = "address";    
  23.        private const string ColumnCountry = "country";    
  24.        private const string ColumnPassword = "password";    
  25.        private const string ColumnEmail = "email";    
  26.        private const string ColumnMobile = "mobile";    
  27.     
  28.        public const string CreateQuery = "CREATE TABLE " + TableName +    
  29.            " ( "    
  30.            + ColumnID + " INTEGER PRIMARY KEY,"    
  31.            + ColumnUsername + " TEXT,"    
  32.            + ColumnFirstName + " TEXT,"    
  33.            + ColumnLastName + " TEXT,"    
  34.            + ColumnAddress + " TEXT,"    
  35.            + ColumnCountry + " TEXT,"    
  36.            + ColumnPassword + " TEXT,"    
  37.            + ColumnEmail + " TEXT,"    
  38.            + ColumnMobile + " TEXT)";    
  39.     
  40.        public const string DeleteQuery = "DROP TABLE IF EXISTS " + TableName;    
  41.     
  42.        public void Register(Context context, UserDetails user)    
  43.        {    
  44.            SQLiteDatabase db = new Helper(context).WritableDatabase;    
  45.            ContentValues Values = new ContentValues();    
  46.            Values.Put(ColumnUsername, user.Username);    
  47.            Values.Put(ColumnFirstName, user.FirstName);    
  48.            Values.Put(ColumnLastName, user.LastName);    
  49.            Values.Put(ColumnCountry, user.Country);    
  50.            Values.Put(ColumnAddress, user.Address);    
  51.            Values.Put(ColumnPassword, user.Password);    
  52.            Values.Put(ColumnEmail, user.Email);    
  53.            Values.Put(ColumnMobile, user.Mobile);    
  54.            db.Insert(TableName, null, Values);    
  55.            db.Close();    
  56.        }    
  57.        public UserDetails Authenticate(Context context, UserDetails user)    
  58.        {    
  59.            SQLiteDatabase db = new Helper(context).ReadableDatabase;    
  60.            ICursor cursor = db.Query(TableName, new string[]     
  61.            { ColumnID, ColumnFirstName,ColumnLastName,ColumnAddress,ColumnCountry, ColumnUsername, ColumnPassword, ColumnEmail, ColumnMobile },    
  62.            ColumnUsername + "=?"new string[] { user.Username }, nullnullnull);    
  63.            if(cursor != null && cursor.MoveToFirst() && cursor.Count > 0)    
  64.            {    
  65.                UserDetails user1 = new UserDetails(cursor.GetString(6));    
  66.                if (user.Password.Equals(user1.Password))    
  67.                return user1;    
  68.            }    
  69.            return null;    
  70.        }    
  71.     
  72.        public List<UserDetails> GetUser(Context context)    
  73.        {    
  74.            List<UserDetails> users = new List<UserDetails>();    
  75.            SQLiteDatabase db = new Helper(context).ReadableDatabase;    
  76.            string[] columns = new string[] {ColumnID,ColumnUsername,ColumnFirstName,ColumnLastName,ColumnAddress,ColumnCountry,ColumnPassword,ColumnEmail,ColumnMobile };    
  77.            using(ICursor cursor = db.Query(TableName, columns, nullnullnullnullnull))    
  78.            {    
  79.                while (cursor.MoveToNext())    
  80.                {    
  81.                    users.Add(new UserDetails    
  82.                    {    
  83.                        ID = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnID)),    
  84.                        Username = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnUsername)),    
  85.                        FirstName = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnFirstName)),    
  86.                        Country = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnCountry)),    
  87.                        Address = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnAddress)),    
  88.                        LastName = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnLastName)),    
  89.                        Password = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnPassword)),    
  90.                        Email = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnEmail)),    
  91.                        Mobile = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnMobile))    
  92.     
  93.                    });    
  94.                }    
  95.                db.Close();    
  96.                return users;    
  97.            }    
  98.        }   
  99.    }   
We have finished step 3. Now we are left with creating activity class w.r.t. Layouts (axml's.)
 
Activities
 
Finally, we will create an activity for passing the data to and from layouts-to-SQLite db. Create a folder Activities first and add the below-listed class files.
 
1. MainActivity..cs (For User Login Activity)
 
  1. [Activity(MainLauncher = true)]  
  2. public class MainActivity : Activity    
  3.     {    
  4.         private EditText txtUsername, txtPassword;    
  5.         private Button btnSignIn, btnCreate;    
  6.         Helper helper;    
  7.         protected override void OnCreate(Bundle savedInstanceState)    
  8.         {    
  9.             base.OnCreate(savedInstanceState);    
  10.     
  11.             // Set our view from the "main" layout resource    
  12.             SetContentView(Resource.Layout.Main);    
  13.                 
  14.             txtUsername = FindViewById<EditText>(Resource.Id.txtusername);    
  15.             txtPassword = FindViewById<EditText>(Resource.Id.txtpassword);    
  16.             btnCreate = FindViewById<Button>(Resource.Id.btnSignUp);    
  17.             btnSignIn = FindViewById<Button>(Resource.Id.btnSign);    
  18.             helper = new Helper(this);    
  19.     
  20.             btnCreate.Click += delegate { StartActivity(typeof(SignUp)); };    
  21.     
  22.             btnSignIn.Click += delegate    
  23.             {    
  24.                 try    
  25.                 {    
  26.                     string Username = txtUsername.Text.ToString();    
  27.                     string Password = txtPassword.Text.ToString();    
  28.                     var user = helper.Authenticate(this,new UserDetails(null,Username,null,null,null,null,null,Password,null));    
  29.                     if (user != null)    
  30.                     {    
  31.                             
  32.                         Toast.MakeText(this"Login Successful", ToastLength.Short).Show();    
  33.                         Intent intent = new Intent(thistypeof(Welcome));    
  34.                         intent.PutExtra("UserName", Username);    
  35.                         StartActivity(typeof(Welcome));    
  36.     
  37.                     }    
  38.                     else    
  39.                     {    
  40.                         Toast.MakeText(this"Login Unsuccessful! Please verify your Username and Password", ToastLength.Short).Show();    
  41.                     }    
  42.                 }    
  43.                 catch (SQLiteException ex)    
  44.                 {    
  45.                     Toast.MakeText(this""+ex, ToastLength.Short).Show();    
  46.                 }    
  47.                     
  48.             };    
  49.         }    
  50.     }    
 
2. SignUp.cs ( For User Registration Activity)
 
  1. [Activity]    
  2.     public class SignUp : Activity    
  3.     {    
  4.         private EditText edtfname,edtlname,edtCountry,edtAddress, edtUsername, edtEmail, edtPassword, edtMobile;    
  5.         private Button btnCreate, btnBack;    
  6.         Helper helper;    
  7.         protected override void OnCreate(Bundle savedInstanceState)    
  8.         {    
  9.             base.OnCreate(savedInstanceState);    
  10.     
  11.             // Create your application here    
  12.             SetContentView(Resource.Layout.SignUp);    
  13.                 
  14.             edtfname = FindViewById<EditText>(Resource.Id.edtfname);    
  15.             edtlname= FindViewById<EditText>(Resource.Id.edtlname);    
  16.             edtCountry= FindViewById<EditText>(Resource.Id.edtCountry);    
  17.             edtAddress= FindViewById<EditText>(Resource.Id.edtAddress);    
  18.             edtUsername = FindViewById<EditText>(Resource.Id.edtusername);    
  19.             edtPassword = FindViewById<EditText>(Resource.Id.edtpassword);    
  20.             edtEmail = FindViewById<EditText>(Resource.Id.edtEmail);    
  21.             edtMobile = FindViewById<EditText>(Resource.Id.edtMobile);    
  22.             btnCreate = FindViewById<Button>(Resource.Id.btnCreate);    
  23.             btnBack = FindViewById<Button>(Resource.Id.btnBack);    
  24.             helper = new Helper(this);    
  25.     
  26.             btnBack.Click += delegate { StartActivity(typeof(MainActivity)); };    
  27.     
  28.             btnCreate.Click += delegate     
  29.             {    
  30.                 UserDetails user = new UserDetails()    
  31.                 {    
  32.                     FirstName = edtfname.Text,    
  33.                     LastName=edtlname.Text,    
  34.                     Address=edtAddress.Text,    
  35.                     Country=edtCountry.Text,    
  36.                     Username = edtUsername.Text,    
  37.                     Password = edtPassword.Text,    
  38.                     Email = edtEmail.Text,    
  39.                     Mobile = edtMobile.Text    
  40.                 };    
  41.                 string username = edtUsername.Text;    
  42.                 string password = edtPassword.Text;    
  43.                 if(string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))    
  44.                 {    
  45.                     Toast.MakeText(this"Username and Password should not be empty.", ToastLength.Short).Show();    
  46.                 }    
  47.                 else    
  48.                 {    
  49.                     helper.Register(this, user);    
  50.                     var data = helper.GetUser(this);    
  51.                     user = data[data.Count - 1];    
  52.                     Toast.MakeText(this, $"User {user.Username} registration successful!", ToastLength.Short).Show();    
  53.                     Clear();    
  54.                     Toast.MakeText(this, $"Total {data.Count} Users.", ToastLength.Short).Show();    
  55.                 }    
  56.             };    
  57.                
  58.         }    
  59.         void Clear()    
  60.         {    
  61.             edtfname.Text = "";    
  62.             edtlname.Text = "";    
  63.             edtAddress.Text = "";    
  64.             edtCountry.Text = "";    
  65.             edtUsername.Text = "";    
  66.             edtPassword.Text = "";    
  67.             edtMobile.Text = "";    
  68.             edtEmail.Text = "";    
  69.         }    
  70.     }    
 
3. Welcome.cs (After log-in Welcome page activity)
 
  1. [Activity(Label = "Welcome")]    
  2.     public class Welcome : Activity    
  3.     {    
  4.         string loggedInUser { getset; }    
  5.            
  6.         private TextView txtWelcome;    
  7.         private Button btnLogout;    
  8.         protected override void OnCreate(Bundle savedInstanceState)    
  9.         {    
  10.             base.OnCreate(savedInstanceState);    
  11.     
  12.             // Create your application here    
  13.             SetContentView(Resource.Layout.Welcome);    
  14.     
  15.             txtWelcome = FindViewById<TextView>(Resource.Id.txtWelcome);    
  16.             txtWelcome.Text = $"Welcome {Intent.GetStringExtra("UserName") ?? "User" }";    
  17.             btnLogout = FindViewById<Button>(Resource.Id.btnLogout);    
  18.     
  19.             btnLogout.Click+= delegate { StartActivity(typeof(MainActivity)); };    
  20.         }    
  21.     }  
 
That's it! Now run the code and see the output. You should be able to Register User, Login, and Logout. And all this information is stored in the SQLite database.
 
 
Happy Coding!