Android Tutorial – Sending Data Using Intent




In this tutorial we will learn how to transfer data like string, int, float etc. from one activity to another activity using intent with android. This is another basic requirement of android app to transfer data between activity to perform many operations and to make application more user friendly.

Example of showing sending data from one activity to another activity

Must See :Android Tutorial: Hello World First Android Project

In this application we create two java class/files named FirstActivity.java and SecondActivity.java and two .xml files named activity_first.xml and activity_second.xml

Now create a new project named as ‘Sending Data’, give the name ‘FirstActivity’ to your class activity and give the name ‘activity_first’ to your xml layout.

And then open manually another class SecondActivity.java in source(src) folder where FirstActivity.java already exists and activity_second.xml in layout folder which is found in the resource (res) folder  in your project.

Edit your Manifest.xml file

First of all, we have to define SecondActivity.java class with in the Manifest.xml because those classes which have layout must be defined with in this xml otherwise our android app will not run and it will result in forced close.

Don’t Miss : Android Tutorial – Using EditText, TextView and Button with Example

<?xml version=“1.0” encoding=“utf-8”?>

<manifest xmlns:android=“http://schemas.android.com/apk/res/android”

package=“com.youthgiri.sendingdata”

android:versionCode=“1”

android:versionName=“1.0” >

 

<uses-sdk

android:minSdkVersion=“8”

android:targetSdkVersion=“17” />

 

<application

android:allowBackup=“true”

android:icon=“@drawable/ic_launcher”

android:label=“@string/app_name”

android:theme=“@style/AppTheme” >

<activity

android:name=com.youthgiri.sendingdata.FirstActivity

android:label=“FirstActivity” >

<intent-filter>

<action android:name=“android.intent.action.MAIN” />

 

<category android:name=“android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

 

<activity

android:name=“com.youthgiri.sendingdata.SecondActivity”

android:label=“SecondActivity” >

</activity>

</application>

 

</manifest>

 

Edit your activity_first.xml

This is layout file for our first class i.e. FirstActivity.java which is defined by relative layout and consist of an EditText to take input/data from the user which will be sent from this activity to SecondActivity.java and a Button to perform/implement this task.

 

<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”

tools:context=“.FirstActivity” >

 

<EditText

android:id=“@+id/editText1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentTop=“true”

android:layout_centerHorizontal=“true”

android:layout_marginTop=“64dp”

android:ems=“10” >

 

<requestFocus />

</EditText>

 

<Button

android:id=“@+id/button1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@+id/editText1”

android:layout_centerHorizontal=“true”

android:layout_marginTop=“39dp”

android:text=“Button” />

 

</RelativeLayout>

 

Edit your FirstActivity.java

This is our FirstActivity.java class in which task of sending data using Intent will performed.

 

package com.youthgiri.sendingdata;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

 

public class FirstActivity extends Activity implements OnClickListener {

 

               

                EditText e;

                Button b;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_first);

        e = (EditText)findViewById(R.id.editText1);

        b = (Button)findViewById(R.id.button1);

        b.setOnClickListener(this);

    }

 

   

 

                @Override

                public void onClick(View arg0) {

                                // TODO Auto-generated method stub

                                // create a new intent

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

                      // put the name and phone(to be sent to other activity) in intent

                      String name = null;

                      name=e.getText().toString();

                      intent.putExtra(“NAME”, name);

                     

                      // start the second activity

                      startActivity(intent);

                               

                }

   

}

 

Edit your activity_second.xml

This is layout file for our second class i.e. SecondActivity.java which is defined by relative layout and consist of a TextView to show the data from the user which is received from FirstActivity.java by SecondActivity.java.

 

<?xml version=“1.0” encoding=“utf-8”?>

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

 

<TextView

android:id=“@+id/textView1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentTop=“true”

android:layout_centerHorizontal=“true”

android:layout_marginTop=“62dp”

android:text=“Large Text”

android:textAppearance=“?android:attr/textAppearanceLarge” />

 

</RelativeLayout>

 

Edit your SecondActivity.java

This is our SecondActivity.java class in which task of receiving data using Intent will performed.

 

package com.youthgiri.sendingdata;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

 

public class SecondActivity extends Activity {

 

TextView t;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_second);

 

t = (TextView)findViewById(R.id.textView1);

 

// extract data from intent

String name=getIntent().getStringExtra(“NAME”);

t.setText(name);

 

}

}

 

Now run your project on emulator.

t3-1

 

t3-2

 

Hope you enjoyed this tutorial and gained information about Android Tutorial – Sending Data Using Intent.  For more details stay connected with us. Also, you can put your queries in Comment Box, we will respond quickly. 

Suggestions BY Google




Be the first to comment

Leave a Reply

Your email address will not be published.


*