Let’s start,

Step 1 - Open Visual Studio, New Project, Templates, Visual C#, Android, Blank App, then select Blank App. Give Project Name and Project Location.



Step 2

Next we need to create Second page, so go to Solution Explorer, Project Name, Resources, layout. After that right click on Add->New Item, then open new Dialog box.



Step 3 -
Select Android Layout and give name Secondpage.axml.



Step 4

And then we need to add one more Activity, so again go to Solution Explorer, then Project Name. After that right click to Add->New Item, then open new Dialog box.

Step 5 - Then select Activity and give name it SecondActivity.cs.



Step 6 -
Next go for Solution Explorer-> Project Name->Resources->layout->Main.axml click to open Design View.

Step 7 - Select Toolbar Drag and Drop Button, TextView and PlainText.





XML Code

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView
  7. android:text="FIRST PAGE"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:id="@+id/textView1" />
  11. <EditText
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:id="@+id/txtpassvalue" />
  15. <Button
  16. android:text="Send"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:id="@+id/btnsend" />
  20. </LinearLayout>

Step 8 - Next open Solution Explorer-> Project Name->Resources->layout->Secondpage.axml click to open Design View.

Step 9 - Select Toolbar Drag and Drop, Button and TextView.





XML Code

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <Button
  7. android:text="GET VALUE"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:id="@+id/btngetvalue" />
  11. <TextView
  12. android:textAppearance="?android:attr/textAppearanceLarge"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:id="@+id/txtgetvalue" />
  16. </LinearLayout>

Step 10 - Next open Solution Explorer-> Project Name->MainActivity.cs click to open Code Window, then give following code,



C# Code

  1. using System;
  2. using Android.App;
  3. using Android.Content;
  4. using Android.Runtime;
  5. using Android.Views;
  6. using Android.Widget;
  7. using Android.OS;
  8. namespace DataValuePass
  9. {
  10. [Activity(Label = "DataValuePass", MainLauncher = true, Icon = "@drawable/icon")]
  11. public class MainActivity: Activity
  12. {
  13. EditText txtdatapass;
  14. Button btnsend;
  15. protected override void OnCreate(Bundle bundle)
  16. {
  17. base.OnCreate(bundle);
  18. // Set our view from the "main" layout resource
  19. SetContentView(Resource.Layout.Main);
  20. // Get our button from the layout resource,
  21. // and attach an event to it
  22. btnsend = FindViewById < Button > (Resource.Id.btnsend);
  23. txtdatapass = FindViewById < EditText > (Resource.Id.txtpassvalue);
  24. btnsend.Click += Button_Click;
  25. }
  26. private void Button_Click(object sender, EventArgs e)
  27. {
  28. var intent = new Intent(this, typeof(SecondActivity));
  29. intent.PutExtra("DATA_PASS", txtdatapass.Text); //DATA_PASS is Identify the Value Pass variable
  30. this.StartActivity(intent);
  31. }
  32. }
  33. }

Step 11 - Next to open Solution Explorer-> Project Name->SecondActivity.cs click to open Code Window, then give following code,



C# Code

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Android.App;
  6. using Android.Content;
  7. using Android.OS;
  8. using Android.Runtime;
  9. using Android.Views;
  10. using Android.Widget;
  11. namespace DataValuePass
  12. {
  13. [Activity(Label = "SecondActivity")]
  14. public class SecondActivity: Activity
  15. {
  16. Button btngetvalue;
  17. TextView txtgevalue;
  18. string txtvalues;
  19. protected override void OnCreate(Bundle savedInstanceState)
  20. {
  21. base.OnCreate(savedInstanceState);
  22. // Create your application here
  23. SetContentView(Resource.Layout.Secondpage);
  24. txtvalues = Intent.GetStringExtra("DATA_PASS"); //Get The Value
  25. btngetvalue = FindViewById < Button > (Resource.Id.btngetvalue);
  26. txtgevalue = FindViewById < TextView > (Resource.Id.txtgetvalue);
  27. btngetvalue.Click += Btngetvalue_Click;;
  28. }
  29. private void Btngetvalue_Click(object sender, EventArgs e)
  30. {
  31. txtgevalue.Text = txtvalues;
  32. }
  33. }
  34. }

Step 12 - Press F5 or Build and Run the Application,

  • First Screen Give the Text Box Value “Hi Welcome" then Click Send Button.Next Move Second Screen.
  • Second Screen to Press Get Button to Retrieve the passing value of “Hi Welcome” to display TextView.

Finally, we have successfully created a Xamarin Android one activity to another activity passing value app.