Passing Value from One activity to another activity in android app using C# with Xamarin

      In this tutorial I will show you how to pass value from one activity to another activity in Android app using C# with Xamarin.


Steps 

  • ·         Create new project

  • ·                Expand resource folder
  •            Click on layout folder  as shown below image
  • ·          Create two layout  Main.axml  and  Datascreen.axml.
  • ·       Create two activity  Mainactivity and activity2.

In Main.axml paste below 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>

    <EditText

        android:layout_width=match_parent

        android:layout_height=wrap_content

        android:id=@+id/editText1 />

    <Button

        android:text=Send Second Activity

        android:layout_width=match_parent

        android:layout_height=wrap_content

        android:id=@+id/button1 />

</LinearLayout>

DataScreen.axml  paste below 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>

    <EditText

        android:layout_width=match_parent

        android:layout_height=wrap_content

        android:id=@+id/editText2 />

    <Button

        android:text=Send to Main Activity

        android:layout_width=match_parent

        android:layout_height=wrap_content

        android:id=@+id/button2 />

</LinearLayout>
MainActivity.cs
public class MainActivity : Activity

    {

        Button btnclick;

        EditText textpass;

        protected override voidOnCreate(Bundle bundle)

        {

            base.OnCreate(bundle);

            // Set our view from the “main” layout resource

            SetContentView (Resource.Layout.Main);

              btnclick = FindViewById<Button>(Resource.Id.button1);

             textpass = FindViewById<EditText>(Resource.Id.editText1);

            btnclick.Click += btn_click;

            string name = Intent.GetStringExtra(“Name”);

            textpass.Text = ” , “ + name;

        

           

        }

        private voidbtn_click(object sender, EventArgs e)

        { 

            if (textpass.Text != “”)

            {

                

                Intent j = new Intent(this, typeof(Activity2));

              

                j.PutExtra(“Name”, textpass.Text.ToString());

               

                StartActivity(j);

            }

            else

            {

                Toast.MakeText(this, “Please Enter Value”, ToastLength.Short).Show();

            }

        }

    }

Activity2.cs

public class Activity2 : Activity

    {

        Button btnsclick;

        EditText textpass2;

        protected override voidOnCreate(Bundle savedInstanceState)

        {

            base.OnCreate(savedInstanceState);

            // Create your application here

            SetContentView(Resource.Layout.DataScreen);

            btnsclick = FindViewById<Button>(Resource.Id.button2);

            textpass2 = FindViewById<EditText>(Resource.Id.editText2);

            string name = Intent.GetStringExtra(“Name”);

            textpass2.Text = ” , “ + name;

            btnsclick.Click += btns_click;

           

        }

        private voidbtns_click(object sender, EventArgs e)

        {

            if (textpass2.Text != “”)

            {

                Intent j = new Intent(this, typeof(MainActivity));

                j.PutExtra(“Name”, textpass2.Text.ToString());

                StartActivity(j);

            }

            else

            {

                Toast.MakeText(this, “Please Enter Value”, ToastLength.Short).Show();

            }

           

        }

    }



Leave a Reply

Your email address will not be published. Required fields are marked *