Xamarin Android: Create Android App One Activity To Another Activity Passing Value

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.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5. android:orientation="vertical"  
  6.   
  7. android:layout_width="fill_parent"  
  8.   
  9. android:layout_height="fill_parent">  
  10.   
  11. <TextView  
  12.   
  13. android:text="FIRST PAGE"  
  14.   
  15. android:layout_width="match_parent"  
  16.   
  17. android:layout_height="wrap_content"  
  18.   
  19. android:id="@+id/textView1" />  
  20.   
  21. <EditText  
  22.   
  23. android:layout_width="match_parent"  
  24.   
  25. android:layout_height="wrap_content"  
  26.   
  27. android:id="@+id/txtpassvalue" />  
  28.   
  29. <Button  
  30.   
  31. android:text="Send"  
  32.   
  33. android:layout_width="match_parent"  
  34.   
  35. android:layout_height="wrap_content"  
  36.   
  37. android:id="@+id/btnsend" />  
  38.   
  39. </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.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5. android:orientation="vertical"  
  6.   
  7. android:layout_width="match_parent"  
  8.   
  9. android:layout_height="match_parent">  
  10.   
  11. <Button  
  12.   
  13. android:text="GET VALUE"  
  14.   
  15. android:layout_width="match_parent"  
  16.   
  17. android:layout_height="wrap_content"  
  18.   
  19. android:id="@+id/btngetvalue" />  
  20.   
  21. <TextView  
  22.   
  23. android:textAppearance="?android:attr/textAppearanceLarge"  
  24.   
  25. android:layout_width="match_parent"  
  26.   
  27. android:layout_height="wrap_content"  
  28.   
  29. android:id="@+id/txtgetvalue" />  
  30.   
  31. </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(thistypeof(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.


Similar Articles