Pass Data Between Two Activities In Xamarin Android App Using Visual Studio 2015


Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In the Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.


  • Visual Studio 2015 Update 3.

The steps, given below, are required to be followed in order to pass the data  between two activities in Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project, or click (Ctrl+Shift+N). The project needs to be clicked after opening all the types of projects in Visual Studio.


Step 2

After opening the New Project, select Installed --> Templates --> Visual C# --> Android --> choose the Blank App (Android).

Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.


Step 3

Next, go to the Solution Explorer and select Resource--> Layout--> double click to open Main.axml page. 


Step 4

After opening the main.axml file, you can either select Designer mode or Source mode, depending on your expertise. We will select the Designer mode here for ease of designing the application.


First, we have to delete the default "Hello World" button and C# button action code for this. So, go to the source panel and delete the coding of the button. Now, go to the MainActivity.cs page and delete the C# code for the button action.

Step 5

Now, go to the Toolbox window where we get all the types of the tools and controls.

Now, scroll down and drag and drop the Button to your application page.


Step 6

Now, go to the Properties window. You need to edit the Button Id Value and Text Value (EX: android:id="@+id/myButton" android:text="@string/hello" ).


Step 7

In this step, go to the Main.axml page Source Panel. Note the Button Id value and Text value.



  1. <LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">  
  2.     <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />  
  3. </LinearLayout>  
Step 8

Next, open the String.xml page. Go to the Solution Explorer-->Resource-->values-->String.xml.


Step 9

After opening the String.xml file, write the following XML code.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Start Activity2</string>  
  4.     <string name="app_name">datapassing</string>  
  5. </resources>  

Step 10

In this step, add one activity called Activity2.cs.

Go to Solution Explorer-->Right Click-->Add-->New Item (Ctrl+shift+A).


Step 11

Now, select the Activity file and give it a name as Activity2.cs. Then, click Add.


Step 12

Go to the MainActivity.cs page and write the following code between OnCreate() Method.
  1. protected override void OnCreate(Bundle bundle)  
  2. {    
  3.     base.OnCreate(bundle);    
  4.     // Set our view from the "main" layout resource    
  5.     SetContentView(Resource.Layout.Main);    
  6.     Button button = FindViewById < Button > (Resource.Id.myButton);    
  7.     button.Click += delegate {    
  8.         var intent = new Intent(thistypeof(Activity2));    
  9.         intent.PutExtra("MyData""Data from Activity1");    
  10.         StartActivity(intent);    
  11.     };    
  12. }    
  1. Templates

Step 13
Go to the Activity2.cs page and write the following code between OnCreate() Method.
  1. protected override void OnCreate(Bundle savedInstanceState) {  
  2.     base.OnCreate(savedInstanceState);  
  3.     // Create your application here  
  4.     string text = Intent.GetStringExtra("MyData") ? ? "Data is not available";  
  5.     Console.WriteLine(text);  
  6. }  

Step 14

If you have Android Virtual device, run the app on it. Else, connect your Android phone and run the app in that.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.



After a few seconds, the app will start running on your phone.

Click the "START ACTIVITY2" Button.


Now, you will see the Activity2 successfully.



So, this was the process of passing the data between two activities in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    View all