Friday, 25 September 2015

Passing The Data Between the Two Activitys Using Intent

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="10dp" />

    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:inputType="number"
       
android:ems="10"
       
android:id="@+id/editText2"
       
android:hint="enter ph number"
       
android:layout_marginTop="10dp" />

    <
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="10dp" />
</
LinearLayout>
MainActivity.Java
---------------------


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 sendb;

    EditText name1,ph1;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

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

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

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

        sendb.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

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

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

                //Converting String to Long

                long l1=Long.parseLong(s2);

                Intent intent=new Intent(getApplicationContext(),RecevingActivity.class);

                //Sending the Data From Intent

                intent.putExtra("name",s1);

                intent.putExtra("ph",l1);

                startActivity(intent);





            }

        });

    }

}


activity_recevingactivity.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.passingthedatausingintent.RecevingActivity"
   
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="10dp" />

    <
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="10dp" />
</
LinearLayout>
RecevingActivity.Java
---------------------------
package com.android.anil.passingthedatausingintent;



import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;



public class RecevingActivity extends AppCompatActivity {

   TextView t1,t2;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_receving);

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

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

        //Getting Intent

            Intent in=getIntent();

            //Receveing data from intent

              String s3=in.getStringExtra("name");

             Long l2=in.getLongExtra("ph", 0);

          //Convert to Long to String

           String s4=Long.toString(l2);

          t1.setText("Name is : "+s3);

           t2.setText("PhNumber: "+s4);
    } }
Output is:
----------------------------



No comments:

Post a Comment