Pass Data between Activities using intent (Java) - Android Studio Tutorial
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"> <EditText android:id="@+id/nameEt" android:hint="Enter Name" android:inputType="text" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/emailEt" android:hint="Enter Email" android:inputType="textEmailAddress" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/phoneEt" android:hint="Enter Phone" android:inputType="phone" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android: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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //actionbar title ActionBar actionBar = getSupportActionBar(); actionBar.setTitle("First Activity"); //EditText final EditText mNameEt = findViewById(R.id.nameEt); final EditText mEmailEt = findViewById(R.id.emailEt); final EditText mPhoneEt = findViewById(R.id.phoneEt); //Button Button mSaveBtn = findViewById(R.id.saveBtn); //button click listener mSaveBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //get data from edit text String name = mNameEt.getText().toString(); String email = mEmailEt.getText().toString(); String phone = mPhoneEt.getText().toString(); //activity intent Intent 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"> <TextView android: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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //actionbar title ActionBar actionBar = getSupportActionBar(); actionBar.setTitle("Second Activity"); //intent to get data Intent intent = getIntent(); String name = intent.getStringExtra("NAME"); String email = intent.getStringExtra("EMAIL"); String phone = intent.getStringExtra("PHONE"); //TextView TextView mResultTv = findViewById(R.id.resultTv); //setText mResultTv.setText("Name: "+name+"\nEmail: "+email+ "\nPhone: "+phone); } }
Comments
Post a Comment