Tuesday, 13 January 2015

Pass data from one activity to another using Intent in Xamarin

In this Article, we will see how to pass data from one activity to another using Intent in Xamarin. In my previous Article, we learnt about Checkbox widget in Xamarin.

Let's Begin:
1. Create a new Android Application.
2. Add a layout (named as Main.axml).
3) Drop TextView, EditText(set id="@+id/txt_Name") and Button(set id="@+id/btn_Submit" and text="Submit") control onto the Main.axml Layout.
4) Add another Layout and name it as Result.axml.
Drop TextView(Large) control on Result.axml layout and set id="@+id/txt_Result".
Activity1.cs Code:
using System;

using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;

namespace IntentDemo
{
    [Activity(Label = "IntentDemo", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        EditText txt_Name;
        Button btn_Submit;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            //Get txt_Name and btn_Submit Button CheckBox control from the Main.xaml Layout. 
            txt_Name = FindViewById<EditText>(Resource.Id.txt_Name);
            btn_Submit = FindViewById<Button>(Resource.Id.btn_Submit);
            //btn_Submit click event
            btn_Submit.Click += btn_Submit_Click;
        }

        void btn_Submit_Click(object sender, EventArgs e)
        {
            //if EditText in not Empty
            if(txt_Name.Text!="")
            {
                //passing the Activity2 in Intent
                Intent i = new Intent(this,typeof(Activity2));
                //Add PutExtra method data to intent.
                i.PutExtra("Name",txt_Name.Text.ToString());
                //StartActivity
                StartActivity(i);
            }
            else
            {
                Toast.MakeText(this,"Please Provide Name",ToastLength.Short).Show();
            }
        }
    }
}
In above Code, We have created an Intent and Bind Activity2 to it. PutExtra method of Intent allows us to store data in Key-Value pairs. We can retrieve the data in the other Activity using Intent. GetTypeExtra method. In above Code, We have passed string using PutExtra() method and for Retrieving the string in another activity, we use Intent.GetStringExtra("Name") method and pass key value in it as a parameter.
Activity2.cs Code:
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;

namespace IntentDemo
{
    [Activity(Label = "Result")]
    public class Activity2 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "Result" layout resource
            SetContentView(Resource.Layout.Result);
            //Get txt_Result TextView control from the Main.xaml Layout. 
            TextView txt_Result = FindViewById<TextView>(Resource.Id.txt_Result);
            //Retrieve the data using Intent.GetStringExtra method
            string name = Intent.GetStringExtra("Name");
            txt_Result.Text ="Hello, "+name;
        }
    }
}
Final Preview:
Hope you like it. Thanks.
[Download Source Code via Google Drive]

0 comments:

Post a Comment

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Subscribe us on YouTube

Like us on Facebook

Follow us on Google+