Introduction
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.
Prerequisites
- 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.
Main.axml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
- <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
- </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.
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Start Activity2</string>
- <string name="app_name">datapassing</string>
- </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.
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
- SetContentView(Resource.Layout.Main);
- Button button = FindViewById < Button > (Resource.Id.myButton);
- button.Click += delegate {
- var intent = new Intent(this, typeof(Activity2));
- intent.PutExtra("MyData", "Data from Activity1");
- StartActivity(intent);
- };
- }
Step 13
Go to the Activity2.cs page and write the following code between OnCreate() Method.
- protected override void OnCreate(Bundle savedInstanceState) {
- base.OnCreate(savedInstanceState);
-
- string text = Intent.GetStringExtra("MyData") ? ? "Data is not available";
- Console.WriteLine(text);
- }
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.
Output
After a few seconds, the app will start running on your phone.
Click the "START ACTIVITY2" Button.
Now, you will see the Activity2 successfully.
Summary So, this was the process of passing the data between two activities in Xamarin Android app, using Visual Studio 2015.