How to pass data from one activity to another in android studio

Android, Android for Beginners, Basics

In this tutorial we will learn 2 basic things.Pass data between activities and add a back arrow. There are multiple ways to pass data between activities in android studio. Today we will use the easiest way to pass data from one activity to another. We will also add add a back arrow in Android studio. So we simply need 2 activities for this example and 3 to 4 lines of code to send and receive data.

So let’s get started.

What we will learn in this tutorial and How to:

  • Add a Back arrow in app-bar
  • Create New Activity in Android Studio
  • Call Another Activity in Android Studio
  • Pass data between two activities in Android Studio
  • Only enable digits to be entered in Edit Text field
  • Call Button’s onClick() Listener in Android Studio
  • and much more…
  • Create or Open Android Studio Project
  • Add EditText fields in 1st activity to get user data
  • Create another receiving Activity
  • Pass data from 1st activity onClick() of button using Intent
  • Receive data using Intent in 2nd activity and show on screen

Create or open Android Studio Project

Design The XML of First Activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="20dp"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/nameET"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Name" />
    <EditText
        android:id="@+id/numberET"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/nameET"
        android:layout_marginTop="10dp"
        android:hint="Name"
        android:inputType="number" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/numberET"
        android:layout_marginTop="30dp"
        android:text="Send Data" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="First Activity!" />
</RelativeLayout>

Code MainActivity.Java to send data

public class MainActivity extends AppCompatActivity {
    EditText name, number;
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Hooks 
        name = findViewById(R.id.nameET);
        number = findViewById(R.id.numberET);
        btn = findViewById(R.id.btn);
        //Pass Data on Button Click 
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Get data from input field 
                String getName = name.getText().toString();
                String getNumber = number.getText().toString();
                //Pass data to 2nd activity 
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("name", getName);
                intent.putExtra("number", getNumber);
                startActivity(intent);
            }
        });
    }
}

Design the 2nd XML layout to show received data

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="20dp"
    tools:context=".SecondActivity">
    <TextView
        android:id="@+id/set_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textSize="24sp" />
    <TextView
        android:id="@+id/set_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/set_name"
        android:layout_marginTop="10dp"
        android:text="123"
        android:textSize="24sp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Second Activity"
        android:textSize="24sp" />
</RelativeLayout>

Code in the 2nd Java to get data

public class SecondActivity extends AppCompatActivity {
    TextView name, number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        //Hooks 
        name = findViewById(R.id.set_name);
        number = findViewById(R.id.set_number);
        //Get text from Intent 
        Intent intent = getIntent();
        String getName = intent.getStringExtra("name");
        String getNumber = intent.getStringExtra("number");
        //Set Text 
        name.setText(getName);
        number.setText(getNumber);
    }
}

Let's Connect

Videos are always a better source for in-depth knowledge. So, connect with me at YouTube.

Download Project

Flutter Login App UI Source Code- How to Create a complete Flutter app - Flutter education app - Flutter app UI Design - Flutter Course - Flutter Complete App 2022
Dashboard Source COde - Dashboard design in flutter - flutter dashoard - flutter app design 2022 - Flutter UI - Flutter homepage

Latest Courses

Flutter Login App UI Source Code- How to Create a complete Flutter app - Flutter education app - Flutter app UI Design - Flutter Course - Flutter Complete App 2022
Learn flutter from scratch in 4 hours - Flutter Crash Course - Coding with T

Latest Tutorials

Flutter Profile page UI Design - Flutter app design 2023 - Profile Screen Flutter UI - Flutter 2023
Flutter Firebase phone authentication - Firebase Phone auth tutorial in flutter - Flutter Firebase phone number authentication 2022
Firebase Authentication in flutter - Flutter firebase authentication 2022 - firebase auth tutorial 2022 - firebase flutter auth
How to setup firebase in flutter 2022 - Flutter firebase setup
Dashboard Source COde - Dashboard design in flutter - flutter dashoard - flutter app design 2022 - Flutter UI - Flutter homepage

Join Our Mailing List

Join our mailing list to receive the latest, Free Courses, Video Tutorials, free codes and updates regarding Tutorials and services. You will also start getting coupons on the related services.

You have Successfully Subscribed!

FLUTTER APP

Learn Flutter App development from scratch. Create LOGIN APP that can be used with any application which requires Login Sign Up functionality.

You can watch it on YouTube or read and get source code from Blog Post.

You have Successfully Subscribed!