Serving Industries Worldwide

Innovative Ways - Satisfied Clientele

Xamarin Android: Create Login Using SQLite Database


Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

 
 

ai-Hiring-banner

Xamarin is an open-source platform for creating modern and performance applications with .NET for iOS, Android, and Windows. This pattern allows developers to write all of their business logic in one language (or reuse existing application code) but achieve original performance, look and feel on each platform.

Table of Content

What is SQLite?

Android SQLite is the most preferred way to store data for Android applications. For many applications, SQLite is the backbone of applications whether used directly or by a third-party wrapper. We will create it today using the Android SQLite database.

It is embedded in Android By default. Therefore, no database setup or administrative work is required.

Now let's go step by step how to create a login form in Xamarin Android

We will create this project in Visual Studio 2019, so you too should try to use the latest versions.


Step 1:Select a new project

Open Visual Studio -> Create New Project ->Android App (Xamarin)

Then click on the Next button. Note the highlight point of the image as shown in the image.We build for mobiles so mobiles need to be selected.

ai-Hiring-banner

Image: Select new project


Step 2: Give the project name

You can give your project the name you want. Enter the name of your project and click on the Create buttonwe have named our project SQLite which you can see in Image.

ai-Hiring-banner

Image: Give project name


Step 3: Select template

From this, you can select whatever you want. All are templates. We only need a single page so we will select a single view app.If you need navigation you can also select the second option navigation drawer which will provide you navigation.

If you need a tab menu, you can also select the third option Tabbed App which will provide you a tab menu.

   

If you need a blank page then select the last option which will be a blank page.Select a low level whenever possible in the minimum Android version as doing so will help you run in Low device.

Then click on OK

ai-Hiring-banner

Image: Select template
 

Now our project is ready as a normal template. Now we will add layout, NuGet package, required class, and interface.


Step 4:Add Layout

Next, go to Solution Explorer-> Project Name (SQLite) ->Resources Folder ->layout Folder. Right-click on Add -> Add item. A new dialog box will open. Select Android the layout, give a name for NewUser.xml and click Add buttonas shown in the image.

Now new Layout Page ready for Design.We will write our design code in this file.

ai-Hiring-banner

Image: Add Layout


Step 5:Add Activity Class

Now we will add an Activity class to our project in which we will write the Register Related Business Logic code.

Go to Solution Explorer-> Project Name (SQLite).Right-click on Add -> Add item. Open new a dialog boxas shown in the image. Select Activity and give name for RegisterActivity.cs. Now, add the New Activity Class.

ai-Hiring-banner

Image: Add Activity Class


Step 6:Add Table

Next, we need to create a database table, so we create a data layer class in the file, we will write the code to do the table, such as id, name, password, etc.

Go to Solution Explorer-> Project Name (SQLite).Right-click to Add -> Add item. Open a new dialog boxas shown in the image. Select Class, give a name for LoginTable.cs

ai-Hiring-banner

Image: Add Table


Step 7:NuGet package

Now we will install the NuGet package.

Go to Solution Explorer (Right -side)-> Project Name (SQLite) Right click -> Select Manage NuGet Packages.

ai-Hiring-banner

Image: NuGet package


Step 8:Install NuGet package

Once the NuGet window opens, click on Browser from the above tab and search for SQLite package and install packages.

 

Planning to Hire Xamarin App Development Company ?

Your Search ends here.

 

This package will help us to connect with the database as well as provide the required methodology.

ai-Hiring-banner

Image: Install NuGet package


Step 9:LoginTable.cs File

Next, Go to Solution Explorer open the LoginTable.cs file, and put the below code.

namespace SQLite
{
classLoginTab
    {
        [PrimaryKey, AutoIncrement]

publicint id { get; set; } 

        [MaxLength(50)] // Maxlength set in UserName column

publicstringuserName{ get; set; }

        [MaxLength(20)] // Maxlength set in passWord column

publicstringpassWord{ get; set; }
    }
}


Step 10:MainActivity.cs File

Next, Go to Solution Explorer open the MainActivity.cs file, and put the below code.

namespace SQLite
{
	publicclassMainActivity :AppCompatActivity
    {
		EditTextuserName;
		EditText Password;
        Button ncreate;
        Button nsign;
	protectedoverridevoidOnCreate(Bundle savedInstanceState)
    {
			base.OnCreate(savedInstanceState);
			Xamarin.Essentials.Platform.Init(this, savedInstanceState);
			SetContentView(Resource.Layout.activity_main);

			Android.Support.V7.Widget.Toolbar toolbar = FindViewById(Resource.Id.toolbar);
			SetSupportActionBar(toolbar);

			FloatingActionButton fab = FindViewById(Resource.Id.fab);
			fab.Click += FabOnClick;

			base.OnCreate(savedInstanceState);
			SetContentView(Resource.Layout.Main);
			// Get our button from the layout resource,  
			// and attach an event to it  
			btnsign = FindViewById


Step 11:RegisterActivity.cs File

Next, Go to Solution Explorer open the RegisterActivity.cs file, and put the below code.

namespace SQLite
{
	EditTextusername;
	EditTexttxtPassword;
    Button ncreate;
    [Activity(Label = "RegisterActivity")]
	publicclassRegisterActivity : Activity
    {
		protectedoverridevoidOnCreate(Bundle savedInstanceState)
        {
			base.OnCreate(savedInstanceState);

			SetContentView(Resource.Layout.Newuser);
			// Create your application here  
			btncreate = FindViewById


Step 12: Output

Finally let’s see the Output

ai-Hiring-banner

Image: Output
 

By this way, we successfully created a login form using the SQLite database.
 

Conclusion

In this blog, we have created a login form for an Android OS using SQLite database.We tried to explain you the step by step method where we can create a normal login form using class, method, interface, database, and package.

ai-Hiring-banner

Xamarin is an open-source platform for creating modern and performance applications with .NET for iOS, Android, and Windows. This pattern allows developers to write all of their business logic in one language (or reuse existing application code) but achieve original performance, look and feel on each platform.

Table of Content

What is SQLite?

Android SQLite is the most preferred way to store data for Android applications. For many applications, SQLite is the backbone of applications whether used directly or by a third-party wrapper. We will create it today using the Android SQLite database.

It is embedded in Android By default. Therefore, no database setup or administrative work is required.

Now let's go step by step how to create a login form in Xamarin Android

We will create this project in Visual Studio 2019, so you too should try to use the latest versions.


Step 1:Select a new project

Open Visual Studio -> Create New Project ->Android App (Xamarin)

Then click on the Next button. Note the highlight point of the image as shown in the image.We build for mobiles so mobiles need to be selected.

ai-Hiring-banner

Image: Select new project


Step 2: Give the project name

You can give your project the name you want. Enter the name of your project and click on the Create buttonwe have named our project SQLite which you can see in Image.

ai-Hiring-banner

Image: Give project name


Step 3: Select template

From this, you can select whatever you want. All are templates. We only need a single page so we will select a single view app.If you need navigation you can also select the second option navigation drawer which will provide you navigation.

If you need a tab menu, you can also select the third option Tabbed App which will provide you a tab menu.

   

If you need a blank page then select the last option which will be a blank page.Select a low level whenever possible in the minimum Android version as doing so will help you run in Low device.

Then click on OK

ai-Hiring-banner

Image: Select template
 

Now our project is ready as a normal template. Now we will add layout, NuGet package, required class, and interface.


Step 4:Add Layout

Next, go to Solution Explorer-> Project Name (SQLite) ->Resources Folder ->layout Folder. Right-click on Add -> Add item. A new dialog box will open. Select Android the layout, give a name for NewUser.xml and click Add buttonas shown in the image.

Now new Layout Page ready for Design.We will write our design code in this file.

ai-Hiring-banner

Image: Add Layout


Step 5:Add Activity Class

Now we will add an Activity class to our project in which we will write the Register Related Business Logic code.

Go to Solution Explorer-> Project Name (SQLite).Right-click on Add -> Add item. Open new a dialog boxas shown in the image. Select Activity and give name for RegisterActivity.cs. Now, add the New Activity Class.

ai-Hiring-banner

Image: Add Activity Class


Step 6:Add Table

Next, we need to create a database table, so we create a data layer class in the file, we will write the code to do the table, such as id, name, password, etc.

Go to Solution Explorer-> Project Name (SQLite).Right-click to Add -> Add item. Open a new dialog boxas shown in the image. Select Class, give a name for LoginTable.cs

ai-Hiring-banner

Image: Add Table


Step 7:NuGet package

Now we will install the NuGet package.

Go to Solution Explorer (Right -side)-> Project Name (SQLite) Right click -> Select Manage NuGet Packages.

ai-Hiring-banner

Image: NuGet package


Step 8:Install NuGet package

Once the NuGet window opens, click on Browser from the above tab and search for SQLite package and install packages.

 

Planning to Hire Xamarin App Development Company ?

Your Search ends here.

 

This package will help us to connect with the database as well as provide the required methodology.

ai-Hiring-banner

Image: Install NuGet package


Step 9:LoginTable.cs File

Next, Go to Solution Explorer open the LoginTable.cs file, and put the below code.

namespace SQLite
{
classLoginTab
    {
        [PrimaryKey, AutoIncrement]

publicint id { get; set; } 

        [MaxLength(50)] // Maxlength set in UserName column

publicstringuserName{ get; set; }

        [MaxLength(20)] // Maxlength set in passWord column

publicstringpassWord{ get; set; }
    }
}


Step 10:MainActivity.cs File

Next, Go to Solution Explorer open the MainActivity.cs file, and put the below code.

namespace SQLite
{
	publicclassMainActivity :AppCompatActivity
    {
		EditTextuserName;
		EditText Password;
        Button ncreate;
        Button nsign;
	protectedoverridevoidOnCreate(Bundle savedInstanceState)
    {
			base.OnCreate(savedInstanceState);
			Xamarin.Essentials.Platform.Init(this, savedInstanceState);
			SetContentView(Resource.Layout.activity_main);

			Android.Support.V7.Widget.Toolbar toolbar = FindViewById(Resource.Id.toolbar);
			SetSupportActionBar(toolbar);

			FloatingActionButton fab = FindViewById(Resource.Id.fab);
			fab.Click += FabOnClick;

			base.OnCreate(savedInstanceState);
			SetContentView(Resource.Layout.Main);
			// Get our button from the layout resource,  
			// and attach an event to it  
			btnsign = FindViewById


Step 11:RegisterActivity.cs File

Next, Go to Solution Explorer open the RegisterActivity.cs file, and put the below code.

namespace SQLite
{
	EditTextusername;
	EditTexttxtPassword;
    Button ncreate;
    [Activity(Label = "RegisterActivity")]
	publicclassRegisterActivity : Activity
    {
		protectedoverridevoidOnCreate(Bundle savedInstanceState)
        {
			base.OnCreate(savedInstanceState);

			SetContentView(Resource.Layout.Newuser);
			// Create your application here  
			btncreate = FindViewById


Step 12: Output

Finally let’s see the Output

ai-Hiring-banner

Image: Output
 

By this way, we successfully created a login form using the SQLite database.
 

Conclusion

In this blog, we have created a login form for an Android OS using SQLite database.We tried to explain you the step by step method where we can create a normal login form using class, method, interface, database, and package.

Leave Comments