Hello readers, Here I am going to create a simple Login App in Xamarin.

Simple Login App in Xamrine Android:

Let’s see how we can create a Simple Login App in Xamrine Android.

Step 1: Open Visual Studio and create a Blank Android App.

Login App

Your solution will look like this.

Login App

Step 2: In Solution Explorer Goto Resource>Layout>Main.axml as shown in below image.

Login App

Write the following code in Main.axml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="center_horizontal">
 <LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:gravity="top"
 android:layout_gravity="center_vertical"
 android:paddingBottom="200dp">
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:id="@+id/UserNameLayout">
 <TextView
 android:text="User Name:"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/lblUserName" />
 <EditText
 android:text=""
 android:layout_width="143.5dp"
 android:layout_height="wrap_content"
 android:id="@+id/txtUserName" />
 </LinearLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:id="@+id/passwordLayout">
 <TextView
 android:text="Password:"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/lblPassword" />
 <EditText
 android:text=""
 android:layout_width="149.0dp"
 android:layout_height="wrap_content"
 android:id="@+id/txtPassword"
 android:password="true"
 android:gravity="top" />
 </LinearLayout>
 <Button
 android:id="@+id/btnLogin"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:text="Sign In" />
 </LinearLayout>
</LinearLayout>

In the above design, we have declared two TextView and EditText for User Name and Password and one Button.

Step 3- In solution Explorer and goto to MainActivity.cs. Write the following code in MainActivity.cs file.

using Android.App;
using Android.Widget;
using Android.OS;

namespace LoginAppXamarin
{
[Activity(Label = "LoginAppXamarin", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
TextView txtUserName = FindViewById<TextView>(Resource.Id.txtUserName);
TextView txtPassword = FindViewById<TextView>(Resource.Id.txtPassword);
Button Send_Button = FindViewById<Button>(Resource.Id.btnLogin); Send_Button.Click += delegate {
if (txtUserName.Text == "admin" && txtPassword.Text == "admin")
{
Toast.MakeText(this, "Welcome", ToastLength.Long).Show();
}
else
{
Toast.MakeText(this, "Invalid Login", ToastLength.Long).Show();
}

};
}
}
}

Run the application.

Login App

Enter admin in User Name and Password Textbox. A Welcome toast will show.

View More:

Conclusion:

I hope this post will help you in learning Xamarine. I would like to have your feedback, Comments and suggestions.

Thank You.

Leave a Comment