XAMARIN TUTORIAL
In this tutorial we will see “How to create a login screen using Xamarin.Android”. This example assumes, you have Xamarin for Android development environment setup already.
To design login screen as shown in the image below, we will be using two EditText for username, password and login button.
Login Screen using Xamarin

Login.axml

xml version="1.0" encoding="utf-8"?>
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2579BF"
    android:orientation="vertical">

    
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="10dp"
        android:orientation="vertical">

        
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:src="@drawable/ic_launcher" />

        
            android:id="@+id/userName"
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:padding="5dp"
            android:background="@android:color/white" />

        
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="#f7f7f7" />

        
            android:id="@+id/password"
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:padding="5dp"
            android:background="@android:color/white"
            android:inputType="textPassword" />

        

Now, let us create an Activity class and use the below code. To make this example simplified, we are just displaying a toast message when login button is clicked.

LoginActivity.cs

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace MyApplication
{
	[Activity (Label = "POIApplication", Theme="@style/android:Theme.Holo.Light.NoActionBar")]
	public class LoginActivity : Activity
	{
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Login);

			//Initializing button from layout
			Button login = FindViewById<Button> (Resource.Id.login);

			//Login button click action
			login.Click += (object sender, EventArgs e) => {
				Android.Widget.Toast.MakeText(this, "Login Button Clicked!", Android.Widget.ToastLength.Short).Show();
			};
		}
	}

Axact

Author

My name is Dave, Am a cool IT Geek, computer analyst and a tutor. I do alot of computer stuffs like programming, web development, blogging, data administrator, computer security and lots more. Feel free to contact me if want more informations and tutorials.

Post A Comment:

0 comments: