Download Code

Xamarin SignIn SignUp Example Android

In this tutorial you will learn how to create Xamarin SignIn SignUp Example Android Application.In this Xamarin SignIn SignUp Example android application you will use JSON Parsing,WebClient to make POST request to web server.

So,let me give some basic information about classes which you will use in this tutorial.

WebClient

Class provides common methods for sending data to and receiving data from a resource identified by a URI.

Now you have to configure your server end which mostly people say localhost.For that purpose you should download XAMPP server.

After installing XAMPP server on your PC.Go to localhost/phpMyAdmin.Create new database(codeclogin) and Table(newuser) in it.

Now within htdocs directory create new folder(Login).Create two php files xamarinsignIn.php and xamarinSignUp.php.

xamarinsignIn.php

Xamarin signin signup example android, xamarin signin signup, xamarin signin signup example, xamarin signin signup example android, sign signup example android, login registration in xamarin android, xamarin login registration android, xamarin webclient, how to make post request xamarin android, how to send data to server xamarin, how to make get request,

xamarinSignUp.php

Xamarin signin signup example android, xamarin signin signup, xamarin signin signup example, xamarin signin signup example android, sign signup example android, login registration in xamarin android, xamarin login registration android, xamarin webclient, how to make post request xamarin android, how to send data to server xamarin, how to make get request,

I have just paste screen shots.Don’t worry i have added code within above download code button.

Now create new android application project.By default you have Main.axml file which you will use for Sign In.You will create another layout for Sign Up(sign_up.axml).

Go to Main.axml layout.Add two Edittext,Button and a Textview.

Main.axml

Xamarin signin signup example android, xamarin signin signup, xamarin signin signup example, xamarin signin signup example android, sign signup example android, login registration in xamarin android, xamarin login registration android, xamarin webclient, how to make post request xamarin android, how to send data to server xamarin, how to make get request,

sign_up.axml

Xamarin signin signup example android, xamarin signin signup, xamarin signin signup example, xamarin signin signup example android, sign signup example android, login registration in xamarin android, xamarin login registration android, xamarin webclient, how to make post request xamarin android, how to send data to server xamarin, how to make get request,

Now first go to SignUp.cs class and do the following steps.

  • Create variables for UI Components and refer their id’s.
  • Apply click event on Button.
  • Create an AsyncTask class.
  • Within RunInBackground() method use WebClient class and URI to upload values to server uri.

SignUp.cs

namespace XamarinLogin
{
	[Activity(Label = "SignUp")]
	public class SignUp : Activity, View.IOnClickListener
	{
		EditText userName, userEmail, userPassword;
		Button signUp;


		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Create your application here
			SetContentView(Resource.Layout.sign_up);
			initialize();
		}
		public void initialize()
		{
			userName = (EditText)FindViewById(Resource.Id.newUserName);
			userEmail = (EditText)FindViewById(Resource.Id.newUserEmail);
			userPassword = (EditText)FindViewById(Resource.Id.newUserPassword);
			signUp = (Button)FindViewById(Resource.Id.signUpBtn);
			signUp.SetOnClickListener(this);
		}

		public void OnClick(View v)
		{
			switch (v.Id)
			{
				case Resource.Id.signUpBtn:
					if (userName.Text.Equals("") && userEmail.Text.Equals("") && userPassword.Text.Equals(""))
					{
						Toast.MakeText(this, "Empty Fields Found", ToastLength.Short).Show();
					}
					else {

						signUpAsync s = new signUpAsync(this);
						s.Execute();
					}
					break;
			}
		}

		public class signUpAsync : AsyncTask<Java.Lang.Object, Java.Lang.Object, Java.Lang.Object>
		{

			SignUp signObj;

			public signUpAsync(SignUp signObj)
			{
				this.signObj = signObj;
			}
			string name,email,pass;

			protected override void OnPreExecute()
			{
				base.OnPreExecute();
				name = signObj.userName.Text;
				email = signObj.userEmail.Text;
				pass = signObj.userPassword.Text;
			}

			protected override Java.Lang.Object RunInBackground(params Java.Lang.Object[] @params)
			{
				WebClient client = new WebClient();
				Uri uri = new Uri("http://your_server_ip/Login/xamarinsignUp.php");
				NameValueCollection parameters = new NameValueCollection();
				parameters.Add("uName", name);
				parameters.Add("uEmail", email);
				parameters.Add("uPass", pass);
				client.UploadValuesAsync(uri, parameters);

				return null;
			}

		}

	}
}

Now go to MainActivity.cs which is SignIn class/activity.Do the following steps.

  • Create variables for UI Component and refer their id’s.
  • Apply Click event on button and on TextView.
  • TextView click will launch Sign Up activity/class.
  • Create AsyncTask class and within RunInBackground() method.
  • Use WebClient class to upload parameters and retrieve response in form of JSON String.
  • Parse JSON String and get the message.

MainActivity.cs

namespace XamarinLogin
{
	[Activity(Label = "XamarinLogin", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity, Android.Views.View.IOnClickListener
	{
		
		TextView signUp;
		EditText email, password;
		Button signIn;
		signInAsync sn;

		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			initialize();
		}
		public void initialize()
		{
			signUp = (TextView)FindViewById(Resource.Id.signUpTxt);
			email = (EditText)FindViewById(Resource.Id.userEmail);
			password = (EditText)FindViewById(Resource.Id.userPass);
			signIn = (Button)FindViewById(Resource.Id.signInBtn);
			signUp.SetOnClickListener(this);
			signIn.SetOnClickListener(this);
		}

		public void OnClick(View v)
		{
			switch (v.Id)
			{
				case Resource.Id.signInBtn:
					sn = new signInAsync(this);
					sn.Execute();
					break;
				case Resource.Id.signUpTxt:
					Intent up = new Intent(this, typeof(SignUp));
					StartActivity(up);
					Finish();
					break;
			}
		}

		public class signInAsync : AsyncTask<Java.Lang.Object, Java.Lang.Object, Java.Lang.Object>
		{
			MainActivity mainActivity;

			public signInAsync(MainActivity mainActivity)
			{
				this.mainActivity = mainActivity;
			}
			string userEmail, userPassword;
			protected override void OnPreExecute()
			{
				base.OnPreExecute();

				userEmail = mainActivity.email.Text;
				userPassword = mainActivity.password.Text;
			}
			protected override Java.Lang.Object RunInBackground(params Java.Lang.Object[] @params)
			{
				
				WebClient client = new WebClient();
				Uri uri = new Uri("http://your_server_ip/Login/xamarinsignIn.php");
				NameValueCollection parameters = new NameValueCollection();
				parameters.Add("uemail", userEmail);
				parameters.Add("pass", userPassword);
				var response = client.UploadValues(uri, parameters);
				var responseString = Encoding.Default.GetString(response);
				JSONObject ob = new JSONObject(responseString);
				if (ob.OptString("success").Equals("1"))
				{
					mainActivity.RunOnUiThread(() =>
					Toast.MakeText(mainActivity, "You Successfully LogIn", ToastLength.Short).Show());
				}
				else {
					mainActivity.RunOnUiThread(() =>
					                           Toast.MakeText(mainActivity, "InValid Email or Password", ToastLength.Short).Show());

				}

				return null;
			}
		}

	}
}

At last go to Android Manifest.xml file and add INTERNET permission.

Now run your xamarin signin signup example android application.

Xamarin signin signup example android, xamarin signin signup, xamarin signin signup example, xamarin signin signup example android, sign signup example android, login registration in xamarin android, xamarin login registration android, xamarin webclient, how to make post request xamarin android, how to send data to server xamarin, how to make get request,

That’s it for this tutorial.

For Java example visit Android Codec.

Thanks.