Hi friends
In this post we will learn how to send data(integer,long,string etc) from one activity to other.
Note: I hope you are familiar with using Buttons and EditTexts if not then go through this post Using Button and EditText in Android.
Some times while working with android we need to send some data from one Activity to other.
Here we will create a project which will have two activities "FirstActivity" and "Second Activity".
We send Name and Phone Number from FirstActivity to Second Activity and display the sent data.
Create a new project.
Donot forget to add SecondActivity in manifest.
Edit your main.xml file add two textviews, two edittexts, and a button.
It should look like (you can just copy the following xml code and paste in main.xml file)
<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">
<TextView
android:text="Name"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<EditText
android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<TextView
android:text="Phone Number"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<EditText
android:id="@+id/phone"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<Button
android:id="@+id/button"
android:text="OK"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
Understanding Android Manifest File of your android app
Working with Linear Layout (With Example)
Nested Linear Layout (With Example)
Table Layout
Frame Layout(With Example)
Absolute Layout
Grid Layout
Activity Life Cycle
Starting Activity For Result
Sending Data from One Activity to Other in Android
Returning Result from Activity
Using CheckBoxes in Android
Using AutoCompleteTextView in Android
Grid View
Adding Radio Buttons In Dialog
Adding Check Boxes In Dialog
Creating Customized Dialogs in Android
Adding EditText in Dialog
Creating Dialog To Collect User Input
How To Receive SMS
Accessing Inbox In Android
Creating Context Menu In Android
How to Forward an Incoming Call In Android
CALL States In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog
Reading and Writing files to SD Card
Creating Table In Android
Inserting, Deleting and Updating Records In Table in Android
How to Create DataBase in Android
Accessing Inbox In Android
In this post we will learn how to send data(integer,long,string etc) from one activity to other.
Note: I hope you are familiar with using Buttons and EditTexts if not then go through this post Using Button and EditText in Android.
Some times while working with android we need to send some data from one Activity to other.
Here we will create a project which will have two activities "FirstActivity" and "Second Activity".
We send Name and Phone Number from FirstActivity to Second Activity and display the sent data.
Create a new project.
Donot forget to add SecondActivity in manifest.
Editing main.xml file
Edit your main.xml file add two textviews, two edittexts, and a button.
It should look like (you can just copy the following xml code and paste in main.xml file)
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"
android:orientation="vertical">
<TextView
android:text="Name"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<EditText
android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<TextView
android:text="Phone Number"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<EditText
android:id="@+id/phone"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<Button
android:id="@+id/button"
android:text="OK"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
Now open the FirstActivity class and write following
FirstActivity.java
public class FirstActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button)findViewById(R.id.button);
final EditText editTextName=(EditText)findViewById(R.id.name);
final EditText editTExtPhone=(EditText)findViewById(R.id.phone);
// add button onClick Listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String name=editTextName.getText().toString();
long phone=Long.parseLong(editTExtPhone.getText().toString());
// create a new intent
Intent intent =new Intent(getApplicationContext(),SecondActivity.class);
// put the name and phone(to be sent to other activity) in intent
intent.putExtra("PERSON_NAME", name);
intent.putExtra("PHONENUMBER", phone);
// start the second activity
startActivity(intent);
}
});
}
}
Edit your SecondActivity class
Point to Note(To Rememeber)
intent.putExtra("PERSON_NAME", name);
we have put the name in intent with "PERSON_NAME" tag, we will fetch name in second activity with same tag(see second activity). similarly we can send and fetch any type of data.
SecondActivity.java (Do not forget to declare this activity in manifest file)
public class SecondActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// extract the name and phone from intent
String name=getIntent().getStringExtra("PERSON_NAME");
long phoneNumber=getIntent().getLongExtra("PHONENUMBER", 0);
TextView tv=new TextView(this);
tv.setTextSize(20);
String str="NAME : "+name+"\nPhone Number : "+phoneNumber;
tv.setText(str);
setContentView(tv);
}
}
Now run the application, you will see the following
More Android Tutorials
Android : Introduction
Eclipse Setup for Android Development
Configuring Eclipse for Android DevelopmentBegging With Android
Creating Your First Android ProjectUnderstanding Android Manifest File of your android app
Working With Layouts
Understanding Layouts in AndroidWorking with Linear Layout (With Example)
Nested Linear Layout (With Example)
Table Layout
Frame Layout(With Example)
Absolute Layout
Grid Layout
Activity
Activity In AndroidActivity Life Cycle
Starting Activity For Result
Sending Data from One Activity to Other in Android
Returning Result from Activity
Working With Views
Using Buttons and EditText in AndroidUsing CheckBoxes in Android
Using AutoCompleteTextView in Android
Grid View
Dialogs In Android
Working With Alert DialogAdding Radio Buttons In Dialog
Adding Check Boxes In Dialog
Creating Customized Dialogs in Android
Adding EditText in Dialog
Creating Dialog To Collect User Input
DatePicker and TimePickerDialog
Using TimePickerDialog and DatePickerDialog In androidWorking With SMS
How to Send SMS in AndroidHow To Receive SMS
Accessing Inbox In Android
ListView:
Populating ListView With DataBaseMenus In Android
Creating Option MenuCreating Context Menu In Android
TelephonyManager
Using Telephony Manager In AndroidWorking With Incoming Calls
How To Handle Incoming Calls in AndroidHow to Forward an Incoming Call In Android
CALL States In Android
Miscellaneous
Notifications In AndroidHow To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog
Storage: Storing Data In Android
Shared Prefferences In Android
SharedPreferences In AndroidFiles: File Handling In Android
Reading and Writing files to Internal StoarageReading and Writing files to SD Card
DataBase : Working With Database
Working With Database in AndroidCreating Table In Android
Inserting, Deleting and Updating Records In Table in Android
How to Create DataBase in Android
Accessing Inbox In Android
Thank You, This helped a bunch.
ReplyDeletegood
ReplyDeletecan u solve this problem sir...
ReplyDeleteI have 2 activity and some edit text and listview ...can u send data second to first in listview ....as like contactlist
Thanks for concept clearing tutorial and examples.
ReplyDeleteNice sir,
ReplyDeleteif we want to set data in second Activity in text view which is declared in activity2.xml file..
then what we have to do.?
ReplyDeleteGreat Article
Android Final Year Project Ideas for Computer Science
Project Centers in Chennai
Mua vé tại đại lý vé máy bay Aivivu, tham khảo
ReplyDeletemua ve may bay di my
giá vé về việt nam
gia ve may bay di da nang
chuyến bay từ hcm ra hà nội
vé máy bay từ sài gòn đi nha trang
rastgele görüntülü konuşma - kredi hesaplama - instagram video indir - instagram takipçi satın al - instagram takipçi satın al - tiktok takipçi satın al - instagram takipçi satın al - instagram beğeni satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - instagram beğeni satın al - instagram beğeni satın al - polen filtresi - google haritalara yer ekleme - btcturk güvenilir mi - binance hesap açma - kuşadası kiralık villa - tiktok izlenme satın al - instagram takipçi satın al - sms onay - paribu sahibi - binance sahibi - btcturk sahibi - paribu ne zaman kuruldu - binance ne zaman kuruldu - btcturk ne zaman kuruldu - youtube izlenme satın al - torrent oyun - google haritalara yer ekleme - altyapısız internet - bedava internet - no deposit bonus forex - erkek spor ayakkabı - webturkey.net - karfiltre.com - tiktok jeton hilesi - tiktok beğeni satın al - microsoft word indir - misli indir
ReplyDeleteaşk kitapları
ReplyDeleteyoutube abone satın al
cami avizesi
cami avizeleri
avize cami
no deposit bonus forex 2021
takipçi satın al
takipçi satın al
takipçi satın al
takipcialdim.com/tiktok-takipci-satin-al/
instagram beğeni satın al
instagram beğeni satın al
btcturk
tiktok izlenme satın al
sms onay
youtube izlenme satın al
no deposit bonus forex 2021
tiktok jeton hilesi
tiktok beğeni satın al
binance
takipçi satın al
uc satın al
sms onay
sms onay
tiktok takipçi satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
instagram beğeni satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
takipcialdim.com/instagram-begeni-satin-al/
perde modelleri
instagram takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
betboo
marsbahis
sultanbet
marsbahis
ReplyDeletebetboo
sultanbet
marsbahis
betboo
sultanbet
eskişehir
ReplyDeleteizmir
muğla
yalova
çanakkale
düzce
antalya
aydın
mate
tiktok jeton hilesi
ReplyDeletetiktok jeton hilesi
referans kimliği nedir
gate güvenilir mi
tiktok jeton hilesi
paribu
btcturk
bitcoin nasıl alınır
yurtdışı kargo
İnstagram takipçi satın al! İnstagram takipçi sitesi ile takipçi satın al sende sosyal medyada fenomen olmaya bir adım at. Sende hemen instagram takipçi satın almak istiyorsan tıkla:
ReplyDelete1- takipçi satın al
2- takipçi satın al
3- takipçi satın al
oncasino
ReplyDeleteHello, I'm happy to see some great articles on your site. Would you like to come to my site later? My site also has posts, comments and communities similar to yours. Please visit and take a look 메이저놀이터
ReplyDeleteIn my opinion, the item you posted is perfect for being selected as the best item of the year. You seem to be a genius to combine 안전놀이터 and . Please think of more new items in the future!
ReplyDeleteThe assignment submission period was over and I was nervous, safetoto and I am very happy to see your post just in time and it was a great help. Thank you ! Leave your blog address below. Please visit me anytime.
ReplyDeleteWhy couldn't I have the same or similar opinions as you? T^T I hope you also visit my blog and give us a good opinion. 안전놀이터
ReplyDeleteThere is no game camp that guarantees that. When do you give a lot? or when giving less But if ดูบอลออนไลน์
ReplyDeleteThe entrance to the pg slot is the entrance to the most popular PG camp slot games. pgslot
ReplyDeleteeven more in the present There is a situation of the Covid-19 epidemic causing many players. Can't travel to play slots at the casino online slots are very popular. pgslot เว็บตรง
ReplyDeletewith changing wild patterns at the start of each spin One to five customers will order food at the bar. ข่าวบอลวันนี้
ReplyDeleteFor playing slots games at night is considered another option. that you can withdraw, ผลบอลสด
ReplyDeleteJust apply for membership with us, including camp. Ready to service 24 hours a day. ดูบอลออนไลน์
ReplyDeleteAnd you are considered very lucky to meet our JILI Slot game camp because it is a game camp that can make money easily overnight only. So you don't need to worry. pgslot
ReplyDeleten ready position muscles are contracted and ready for action. To move, muscles must be relaxed pgslot
ReplyDeleteor newcomers can be a part of slots games that can be played and get real money, low budget, low capital, don't worry because slots pgslot
ReplyDeletePay real money that is easy to use, slots, transfer through wallets, no minimums, come to everyone. pgslot
ReplyDeleteadd more fun with Direct web slots, hard break, fast break, deposit-withdrawal, no minimum ดูบอลออนไลน์
ReplyDeletebetting method in order to increase the chances of winning a bet. for themselves by those ฟุตบอลโลก 2022
ReplyDeletetransactions with our direct web slots by yourself. No need to talk to admin. and no longer have to ambslot
ReplyDeleteKnow how to plan investment bets from playing games, no matter what game it is, it will rely on the pgslot
ReplyDeleteBecome one of Direct web, no minimum, 24 hour service. Deposit-withdrawal, modern auto, give away free credit every day, play games for free ข่าวบอลวันนี้
ReplyDelete