Hello readers, Here I am going to show you how we can Pass Data Between Activity window in Xamarin Android.
How to Pass Data Between Activity Window in Xamarin Android?
This is a most common situation to pass data between application pages. In Web applications, we have different approaches like Query String, Sessions etc to Pass Data Between Activity pages. Here I am going to show you how to pass data between Xamarin Activity. Follow the steps.
Step 1- Open Visual Studio and create Blank App called PassingDataBetweenActivity as shown in below image.
Your solution explorer will look like this.
Step 2- Goto Solution Explorer>Resource>Layout>Main.xaml. Add a button to Layout. Here is the code for Main.xaml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px"> <Button android:text="Goto Second Activity" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnGotoSecond" /> </LinearLayout>
Step 3- Add a listener for the button that we have added in Step 2. Goto Solution Explorer and open MainActivity.cs file and write the following code.
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 PassingDataBetweenActivity { [Activity(Label = "SecondActivity")] public class SecondActivity : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.SecondLayout); string sData = Intent.GetStringExtra("WelcomeText"); TextView lblWelcomeText = FindViewById<TextView>(Resource.Id.lblWelcomeText); lblWelcomeText.Text = sData; // Create your application here } } }
Step 4- Now add another Activity called SecondActivity. For this Right Click on Solution Explorer and choose Add New Item.
Step 5- Goto Solution Explorer>Resource and right click on Layout folder and Add a new Layout called SecondLayout.
Step 6- Goto SecondLayout.axml file and write the following code.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px"> <TextView android:text="Text" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lblWelcomeText" /> </LinearLayout>
As you can see we have added a TextView that will show the data from MainActivity.
Step 7- Open SecondActivity.cs file and write the following code.
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 PassingDataBetweenActivity { [Activity(Label = "SecondActivity")] public class SecondActivity : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.SecondLayout); string sData = Intent.GetStringExtra("WelcomeText"); TextView lblWelcomeText = FindViewById<TextView>(Resource.Id.lblWelcomeText); lblWelcomeText.Text = sData; // Create your application here } } }
Where,
Intent.GetStringExtra(“WelcomeText”) is used to get parameter value from first activity.
Now run the application. Your output window will look like this.
Click on the button.
Download Source Code here.
View More:
- Simple Login App in Xamarin Android.
- Forms in Node.Js.
- Hello World Example in Node.JS.
- Hello World Application using Angular 2.
Conclusion:
I hope this would be the helpful tutorial for you. Your feedback, Comments, and suggestion are always welcome.
Thank You.
Many a times it would so occur that 1 has good cards at fingers and is too thrilled to raise the pot. Perform online but get the feel of real on line casino at no cost.
http://bambulapharmacy.com
https://levitraye.com
You actually revealed that exceptionally well! cheap viagra online
http://sansordonnancefr.com – levitra
blood pressure goes to 97/58 and when i stand it goes to 110/97? viagra
http://withoutscript.com – how to get viagra cheap
You revealed this very well. generic cialis
how do drs determine ifyou have heart disease viagra without doctor prescription
https://tadalafilrembo.com – generique du cialis 5mg
http://canadianvolk.com
https://viaprescription.com/