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

by | Dec 3, 2019 | Android, Android for Beginners, Basics | 0 comments

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…

Steps to pass data between activities

  • 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);
    }
}

CODING with T

🚀 Supercharge your Flutter skills! Subscribe to my YouTube channel now for mind-blowing coding insights, expert tips, and exclusive content. Don’t miss out!

COURSES

Android firebase app - android app 2022 - firebase android app - bulls rent app - coding with t app - android app
android complete login signup product by taimoor sikander free for any type of android app
Flutter E Commerce App Modern and latest
Flutter Login App UI Source Code - Flutter Complete App - Flutter App Design - Flutter App 2023 - Complete Flutter App 2023
Learn flutter from scratch in 4 hours - Flutter Crash Course - Coding with T

Latest Tutorial

How to create a Custom Appbar in flutter. Custom Appbar Design. Flutter App Design
How to create a Custom Shape in Flutter. Flutter E Commerce app Design. Ecommerce app design Flutter. Flutter clippath tutorial
Bottom Navigation bar in Flutter. Flutter Material 3 bottom navigation bar. How to design background color of Flutter bottom navigation bar. Flutter ecommerce app design
Access Admin Panel, Premium Tutorials, Live Chat, 50% off Codes, and More on Patreon!
This is default text for notification bar