Friday, 25 September 2015

Passing The Data Between The Two Activitys Using Bundle

activity_main.xml
-----------------
<LinearLayout
   
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"
   
tools:context=".MainActivity"
   
android:orientation="vertical">


    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText"
       
android:hint="enter name"
       
android:layout_marginTop="5dp" />

    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText2"
       
android:hint="enter location"
       
android:layout_marginTop="5dp" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Send"
       
android:id="@+id/button"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="15dp" />
</
LinearLayout>
MainActivity.Java
--------------------
package com.android.anil.datapassusingbundle;



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 MainActivity extends AppCompatActivity {

    Button sendbtn;

    EditText uname,uloc;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        sendbtn=(Button)findViewById(R.id.button);

        uname=(EditText)findViewById(R.id.editText);

        uloc=(EditText)findViewById(R.id.editText2);

        sendbtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                String s1=uname.getText().toString();

                String s2=uloc.getText().toString();

                //create Bundle class

                Bundle bundle=new Bundle();

                //Sending the data from bundle

                bundle.putString("name",s1);

                bundle.putString("loc",s2);

                Intent intent=new Intent(MainActivity.this,ReceivingActivity.class);

                //Storing the bundle objects into intent

                intent.putExtras(bundle);

                startActivity(intent);





            }

        });



    }



}
activity_receivngactivity.xml
--------------------------
<LinearLayout
   
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"
   
tools:context="com.android.anil.datapassusingbundle.ReceivingActivity"
   
android:orientation="vertical">


    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Large Text"
       
android:id="@+id/textView"
       
android:layout_marginTop="5dp" />

    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Large Text"
       
android:id="@+id/textView2"
       
android:layout_marginTop="9dp" />

</
LinearLayout>

ReceivingActivity.Java
----------------------------------
package com.android.anil.datapassusingbundle;



import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;



public class ReceivingActivity extends AppCompatActivity {

    TextView t1,t2;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_receiving);

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

        t2=(TextView)findViewById(R.id.textView2);

        //Getting Intent

        Intent in=getIntent();

        //Getting Bundle

        Bundle b1=in.getExtras();

        //Receivng the data from bundle

        String s3=b1.getString("name");

        String s4=b1.getString("loc");

        t1.setText(s3);

        t2.setText(s4);

        

    }

}

Output is:
------------------







No comments:

Post a Comment