Header Ads

ad728
  • Breaking News

    Passing Data Between Activities Using Intent

    main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.blogspot.csdevbin.passingdatabetweenactivities.Main">

        <EditText
    Data passing using Intent
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_alignParentTop="true"
            android:layout_marginTop="68dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="send"
            android:id="@+id/button"
            android:layout_below="@+id/editText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="46dp" />
    </RelativeLayout>
    next.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.blogspot.csdevbin.passingdatabetweenactivities.Next">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="data from main"
            android:id="@+id/textView"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="112dp" />
    </RelativeLayout>
    Data passing using Intent


    Main.java
    package com.blogspot.csdevbin.passingdatabetweenactivities;

    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    public class Main extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            final EditText editText=(EditText)findViewById(R.id.editText);
            Button button =(Button)findViewById(R.id.button);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String name =editText.getText().toString();

                    Intent in = new Intent(Main.this,Next.class);
                    in.putExtra("name",name);
                    startActivity(in);
                }
            });
        }
    }


    Next.java
    package com.blogspot.csdevbin.passingdatabetweenactivities;

    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;

    public class Next extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.next);

            TextView textView =(TextView)findViewById(R.id.textView);

            Intent in= getIntent();

            String name =in.getStringExtra("name");
            textView.setText("Data passed is : "+name);
        }
    }




    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728