Download Code

Xamarin Pass Data Activities Android

In this tutorial you will learn how to create xamarin pass data activities android application.Let me give you introduction about bundle and how you will pass data.

Bundle

Bundles is basically used to convert string into a form which can be parsed. There are a lot of public and inherited methods of bundle but in this tutorial you will use putString() which contains key value pair.

You will also use Intent to start new activity and also put key using putExtra() method with that Intent.If you are not familiar with how to start an activity go to that tutorial first.

You will use Edit Text to get user input using first activity,then pass that user input to second activity,summed up and display in a Toast.

Now create new android application project.

Go to your Main.axml layout file,add edit text and a button.

Xamarin Pass Data,Xamarin Pass Data Activities,Xamarin Pass Data Activities Android,Pass Data Activities Android,

Next,create new layout file which will display received data using TextView.Right click on layout > Add File > Layout.Also create class for this layout and call setContentView() method.

Xamarin Pass Data,Xamarin Pass Data Activities,Xamarin Pass Data Activities Android,Pass Data Activities Android,

Go to your MainActivity.cs class and create variable’s for edit text and button.Apply click event on button and start new activity.When starting new activity using intent pass data within intent using putExtra() method.

MainActivity.cs

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

namespace PassData
{
	[Activity (Label = "PassData", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		
		Button myBtn;
		EditText userTxt;
		string myTxt;

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

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);
			initialize ();   // method to refer all id's at once
			// click event on button
			myBtn.Click += delegate(object sender, System.EventArgs e) {
				myTxt = userTxt.Text.ToString();   // getting text from edittext
				var act = new Intent(this,typeof(Home));    // creating intent with activity name
				act.PutExtra("data",myTxt);     // passing data with key which will identify data
				StartActivity(act);              // method to start intent

			};


		}
		// referring id's method
		public void initialize(){
			myBtn = FindViewById< Button > (Resource.Id.myButton);
			userTxt = FindViewById< EditText > (Resource.Id.editText1);
		}
	}
}

Now in the second activity(Home.cs) we will receive the data using intent method getStringExtra().

Home.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

namespace PassData
{
	[Activity (Label = "Home")]			
	public class Home : Activity
	{
		TextView rev;
		String receivedTxt;
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			// Create your application here
			SetContentView(Resource.Layout.home);
			receivedTxt = Intent.GetStringExtra("data") ?? "Data not available";   // using intent with getstring extra method to get data
			rev = FindViewById< TextView > (Resource.Id.textView1);
			rev.Text = receivedTxt;   // setting text to text view
		}
	}
}


Run your xamarin pass data activities android  application project.

Xamarin Pass Data,Xamarin Pass Data Activities,Xamarin Pass Data Activities Android,Pass Data Activities Android,

That’s it for xamarin pass data activities android tutorial.I hope you like it.

Thanks