This example demonstrate about How to send data from one activity to another in Android using bundle.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
activity_bundle.xml
step 1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp"
tools:context=".sendrecieve.BundleActivity">
<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/edtibg"
android:hint="@string/enter_name"
android:inputType="textPersonName"
android:paddingStart="10dp" />
<EditText
android:id="@+id/edtEmail"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:inputType="textEmailAddress"
android:background="@drawable/edtibg"
android:hint="@string/enter_email_address"
android:paddingStart="10dp" />
<EditText
android:id="@+id/edtRollNo"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:inputType="number"
android:background="@drawable/edtibg"
android:hint="@string/enter_roll_number"
android:paddingStart="10dp" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:backgroundTint="@color/btnBg"
android:text="@string/submit"
android:textColor="@color/white"
android:textSize="18sp" />
</LinearLayout>
BundleActivity.java
step 2:
package com.sandhya.androidallproject2023.sendrecieve;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.sandhya.androidallproject2023.R;
public class BundleActivity extends AppCompatActivity {
EditText edtName,edtEmail,edtRollNo;
AppCompatButton btnSubmit;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bundle);
edtName = findViewById(R.id.edtName);
edtEmail = findViewById(R.id.edtEmail);
edtRollNo = findViewById(R.id.edtRollNo);
btnSubmit = findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = edtName.getText().toString();
String email = edtEmail.getText().toString();
String rollNo = edtRollNo.getText().toString();
Intent intent = new Intent(getApplicationContext(),RecieveActivity.class);
intent.putExtra("name",name);
intent.putExtra("email",email);
intent.putExtra("Roll No",rollNo);
startActivity(intent);
}
});
}
}
activity_recieve.xml
step 3:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".sendrecieve.RecieveActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Name"
android:textSize="25dp"
android:paddingStart="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/edtibg"
android:id="@+id/txtName"/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Email"
android:paddingStart="10dp"
android:textSize="25dp"
android:layout_marginTop="10dp"
android:background="@drawable/edtibg"
android:id="@+id/txtEmail"/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Roll Number"
android:paddingStart="10dp"
android:textSize="25dp"
android:layout_marginTop="10dp"
android:background="@drawable/edtibg"
android:id="@+id/txtRollNo"/>
</LinearLayout>
RecieveActivity.xml
step 4:
package com.sandhya.androidallproject2023.sendrecieve;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.sandhya.androidallproject2023.R;
public class RecieveActivity extends AppCompatActivity {
TextView txtName,txtEmail,txtRollNo;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recieve);
txtName = findViewById(R.id.txtName);
txtEmail = findViewById(R.id.txtEmail);
txtRollNo = findViewById(R.id.txtRollNo);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String email = intent.getStringExtra("email");
String rollNo = intent.getStringExtra("Roll No");
txtName.setText("Name : "+name);
txtEmail.setText("Email : "+email);
txtRollNo.setText("Roll No. : "+rollNo);
}
}
output :-
bundle img
recieve img
Generating Download Link...
إرسال تعليق