Transfer data with Intents :
We can easily transfer data from one activity to
another activity with the use of Intents
in Android Application. In this example I am sending the name (set in edit
text) from one activity and will use that name in another activity.
Create a new project in Eclipse : File => New => Android
Application Project and give the package name as com.javalanguageprogramming.datatransferfromactivitydemo.
Copy the code in activity_main.xml shown below :
<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"
android:orientation="vertical"
android:background="#ced3e2"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Your Name"
android:textSize="18sp" />
<EditText
android:id="@+id/nameEdit"
android:layout_width="180dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp" />
</LinearLayout>
<Button
android:id="@+id/sendButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Send" />
</LinearLayout>
In the above xml file a text view is used to give
the label “your name” and edit text is used for entering the name and a button
is used to send the name to another activity.
Copy the code in activity_receive_data.xml shown below :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ced3e2"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="Hey!"
android:textSize="23sp" />
<TextView
android:id="@+id/nameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Welcome to"
android:textSize="23sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="http://javalanguageprogramming.blogspot.in"
android:textSize="20sp" />
</LinearLayout>
In the above code 4 text views are used, out of one
is used for the name which is coming from MainActivity.
Copy the code in MainActivity.java shown below :
package com.javalanguageprogramming.datatransferfromactivitydemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText name;
Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize variables
name = (EditText)findViewById(R.id.nameEdit);
send = (Button)findViewById(R.id.sendButton);
//set on click listener on button
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// check if the edit text is empty or not
if(name.getText() != null || !name.getText().equals("")){
//make an intent
Intent intent = new Intent(MainActivity.this, ReceiveDataActivity.class);
//put data in intent
intent.putExtra("NAME", name.getText().toString());
//clear the edit text so that after user come back he will again enter text
name.setText("");
//start another activity
startActivity(intent);
}
}
});
}
}
In this activity, putExtra method of Intent type is
used to send the data from this activity to another activity.
Copy the code of ReceiveDataActivity.java shown below :
package com.javalanguageprogramming.datatransferfromactivitydemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ReceiveDataActivity extends Activity{
TextView nameText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive_data);
//initialize text view
nameText = (TextView)findViewById(R.id.nameText);
//get the intent
Intent intent = getIntent();
//get the name with the use of intent
String name = intent.getStringExtra("NAME");
//set the text view
nameText.setText(name);
}
}
In this activity, getStringExtra method of intent
type is used to receive data in this activity that came from previous activity.
Do not forget to add ReceiveDataActivity in
AndroidManifest. The code of AndroidManifest
is shown below :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javalanguageprogramming.datatransferfromactivitydemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.javalanguageprogramming.datatransferfromactivitydemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.javalanguageprogramming.datatransferfromactivitydemo.ReceiveDataActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
Thank u for Sharing a good and useful information. Keep Sharing These type of articles
ReplyDeleteHadoop Training in Hyderabad
Java Training in Hyderabad
Your new valuable key points imply much a person like me and extremely more to my office workers. With thanks; from every one of us.
ReplyDeleteAWS Training in Chennai
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
ReplyDeleteangularjs Training in bangalore
angularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
angularjs Training in online
Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
ReplyDeletepython training in velachery | python training institute in chennai
Really great post, I simply unearthed your site and needed to say that I have truly appreciated perusing your blog entries.
ReplyDeleteJava training in Marathahalli | Java training in Btm layout
Java training in Jaya nagar | Java training in Electronic city
Really great post, Thank you for sharing This knowledge.Excellently written article, if only all bloggers offered the same level of content as you, the internet would be a much better place. Please keep it up!
ReplyDeleteData Science Training in Chennai | Data Science training in anna nagar
Data Science training in chennai | Data science training in Bangalore
Data Science training in marathahalli | Data Science training in btm layout
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeleteapple service center chennai | Mac service center in chennai | ipod service center in chennai | Apple laptop service center in chennai
I really like the dear information you offer in your articles. I’m able to bookmark your site and show the kids check out up here generally. Im fairly positive theyre likely to be informed a great deal of new stuff here than anyone
ReplyDeletedevops online training
aws online training
data science with python online training
data science online training
rpa online training
Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision.
ReplyDeleteMicrosoft Azure online training
Selenium online training
Java online training
Python online training
uipath online training
I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
ReplyDeleteJava Training in Bangalore
I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks
ReplyDeletemg university bcom result
rajasthan university bcom final year result
Helpful article , here is the best web development company in Mohali "
ReplyDeleteGreat post. Thanks for sharing your brain. Shershaah Full Movie Download & Review Check mine out sometime
ReplyDeleteOnce I initially commented I clicked the -Notify me when new feedback are added- checkbox and now every time a comment is added I get 4 emails with the identical comment. Is there any way you possibly can take away me from that service? Thanks! apple kundendienst berlin
ReplyDeleteAre you eager to enhance your Java programming skills and stay ahead in the competitive IT landscape? Look no further! Our Java Frameworks Training Course in Noida is meticulously crafted to empower you with the latest tools and techniques in Java development. Noida, a thriving IT hub, provides the perfect backdrop for your learning journey.
ReplyDelete