Description
In this tutorial i'll show you how to pass data between different activities using intent.For Example in this tutorial
Activity 1: Input data using EditText and click Save Button to start second activity with the data that was input.
Activity 2: Show the input data of previous activity using Intent.
Video
Source Code
Step 1: Create a new project OR Open your project
Step 2: Create New Activity File>New>Activity>EmptyActivity
Step 3: Code
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"tools:context=".MainActivity"android:orientation="vertical"android:padding="10dp"android:gravity="center"><EditTextandroid:id="@+id/nameEt"android:hint="Enter Name"android:inputType="text"android:layout_width="match_parent"android:layout_height="wrap_content" /><EditTextandroid:id="@+id/emailEt"android:hint="Enter Email"android:inputType="textEmailAddress"android:layout_width="match_parent"android:layout_height="wrap_content" /><EditTextandroid:id="@+id/phoneEt"android:hint="Enter Phone"android:inputType="phone"android:layout_width="match_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/saveBtn"android:text="Save"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>
MainActivity.java
package com.blogspot.devofandroid.myapplication;import android.content.Intent;import android.support.v7.app.ActionBar;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//actionbar titleActionBar actionBar = getSupportActionBar();actionBar.setTitle("First Activity");//EditTextfinal EditText mNameEt = findViewById(R.id.nameEt);final EditText mEmailEt = findViewById(R.id.emailEt);final EditText mPhoneEt = findViewById(R.id.phoneEt);//ButtonButton mSaveBtn = findViewById(R.id.saveBtn);//button click listenermSaveBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//get data from edit textString name = mNameEt.getText().toString();String email = mEmailEt.getText().toString();String phone = mPhoneEt.getText().toString();//activity intentIntent intent = new Intent(MainActivity.this , SecondActivity.class);intent.putExtra("NAME" , name);intent.putExtra("EMAIL" , email);intent.putExtra("PHONE" , phone);startActivity(intent);}});}}
acitivty_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:gravity="center"android:padding="10dp"tools:context=".SecondActivity"><TextViewandroid:id="@+id/resultTv"android:textSize="30sp"android:textStyle="bold"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>
SecondActivity.java
package com.blogspot.devofandroid.myapplication;import android.content.Intent;import android.support.v7.app.ActionBar;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;public class SecondActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);//actionbar titleActionBar actionBar = getSupportActionBar();actionBar.setTitle("Second Activity");//intent to get dataIntent intent = getIntent();String name = intent.getStringExtra("NAME");String email = intent.getStringExtra("EMAIL");String phone = intent.getStringExtra("PHONE");//TextViewTextView mResultTv = findViewById(R.id.resultTv);//setTextmResultTv.setText("Name: "+name+"\nEmail: "+email+ "\nPhone: "+phone);}}
0 Response to "Pass Data Between Activities Using Intent (Java) - Android Studio Tutorial"