How To Pass Data from One Activity To Another Activity || Android Studio Tutorial [2022]

 Here is the tutorial on how to pass data from one activity to another in android studio.



Code Snippet:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aarappstudios.passdatabetweenactivity">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PassDataBetweenActivity">
<activity
android:name=".SecondActivity"
android:exported="true" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


MainActivity.java

package com.aarappstudios.passdatabetweenactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

private EditText nameEditText,ageEditText;
private Button sendButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

nameEditText=findViewById(R.id.editTextName);
ageEditText=findViewById(R.id.editTextAge);
sendButton=findViewById(R.id.sendButton);

sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name=nameEditText.getText().toString();
int age= Integer.parseInt(ageEditText.getText().toString());

Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("keyname",name);
intent.putExtra("keyage",age);

startActivity(intent);

}
});




}
}


activity_main.xml:

<?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"
android:layout_margin="16dp"
tools:context=".MainActivity">


<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Name" />



<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Age"
android:layout_marginTop="8dp"
/>

<Button
android:id="@+id/sendButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Data"
android:layout_marginTop="8sp"
/>


</LinearLayout>


SecondActivity.java:

package com.aarappstudios.passdatabetweenactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

TextView nameTextView,ageTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

nameTextView=findViewById(R.id.nameTextView);
ageTextView=findViewById(R.id.ageTextView);

String name;
int age;

name=getIntent().getStringExtra("keyname");
age=getIntent().getIntExtra("keyage",0);

nameTextView.setText("Name : " +name);
ageTextView.setText("Age : " + age);





}
}


activity_second.xml

<?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"
android:layout_margin="16dp"
tools:context=".SecondActivity">


<TextView
android:id="@+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Name : " />

<TextView
android:id="@+id/ageTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textSize="16sp"
android:text="Age : " />
</LinearLayout>




Post a Comment

0 Comments