Hello and welcome! Today, we’re going to talk about a commonly encountered topic in the world of Android development: ‘Parcelable’.
In Android, data transfer is quite common. We often come across situations where we need to send data from one activity or fragment to another, or pass a data model from one activity to another. In such data transfer operations, the data needs to be serialized and deserialized. This is where Parcelable comes into play.
Parcelable is an interface used in the Android platform, which allows an object to be broken down into parts and then reassembled into another object. Parcelable can be thought of as an alternative to Serializable, but it is more performant and optimized.
Step 1: Setting up the Project
To begin, let’s set up our project in Android Studio. Open Android Studio and create a new project, providing a suitable project name, package name, and selecting the minimum SDK version based on your target audience.

Step 2: Designing the Layout
Before creating the Parcelable class, let’s start by designing the layout as shown in the video. We’ll need to create a layout that includes three EditText objects for gathering user input. Additionally, we’ll add a button to facilitate the transition between activities.

activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <?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:padding="20dp" tools:context=".MainActivity"> <EditText android:id="@+id/editTextFirstName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:hint="First Name" /> <EditText android:id="@+id/editTextLastName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:hint="Last Name" /> <EditText android:id="@+id/editTextAge" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="Age" android:inputType="number" /> <Button android:id="@+id/btnSendData" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send Data" /> </LinearLayout> |
Step 3: Creating the Parcelable Class
Once the layout is ready, we can proceed to create the Parcelable class. This class will represent the data we want to transfer between components. We’ll define the necessary properties, such as the fields to hold the values from the EditText objects. We’ll also include a constructor and the required getter and setter methods.
User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | public class User { private String firstname; private String lastname; private int age; public User(String firstname, String lastname, int age) { this.firstname = firstname; this.lastname = lastname; this.age = age; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
After defining the Parcelable class, we’ll implement the Parcelable interface by adding the necessary methods: ‘writeToParcel’ and ‘createFromParcel’. These methods will handle the serialization and deserialization of our object.


Now that our Parcelable class is complete, we can move on to the next steps of the tutorial.
User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | import android.os.Parcel; import android.os.Parcelable; import androidx.annotation.NonNull; public class User implements Parcelable { private String firstname; private String lastname; private int age; public User(String firstname, String lastname, int age) { this.firstname = firstname; this.lastname = lastname; this.age = age; } protected User(Parcel in) { firstname = in.readString(); lastname = in.readString(); age = in.readInt(); } public static final Creator<User> CREATOR = new Creator<User>() { @Override public User createFromParcel(Parcel in) { return new User(in); } @Override public User[] newArray(int size) { return new User[size]; } }; public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(@NonNull Parcel dest, int flags) { dest.writeString(firstname); dest.writeString(lastname); dest.writeInt(age); } } |
Step 4: Using Parcelable in Activities or Fragments
Now that we have our Parcelable class ready, we can use it to transfer data between activities or fragments. Let’s see how we can achieve this.
In our code, we have defined EditText and Button objects, namely ‘edFirstName’, ‘edLastName’, ‘edAge’, and ‘btnSend’. We retrieve references to these UI elements using the ‘findViewById’ method.
Next, we set an onClickListener on the ‘btnSend’ button. When the button is clicked, the onClick method is triggered. Inside this method, we retrieve the values entered by the user from the EditText objects, ‘edFirstName’, ‘edLastName’, and ‘edAge’.
Using these values, we create a new instance of our Parcelable class, ‘User’, and pass the collected data as parameters to its constructor.
To transfer this Parcelable object to another activity, we create an intent by specifying the current activity, ‘MainActivity’, and the target activity, ‘UserActivity’. We add the Parcelable object as an extra to the intent using the ‘putExtra’ method, with a key-value pair where the key is ‘user’ and the value is our Parcelable object.
Finally, we start the ‘UserActivity’ by calling ‘startActivity(intent)’, which initiates the transition to the receiving activity, along with the Parcelable data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { EditText edFirstName,edLastName,edAge; Button btnSend; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edFirstName = findViewById(R.id.editTextFirstName); edLastName = findViewById(R.id.editTextLastName); edAge = findViewById(R.id.editTextAge); btnSend = findViewById(R.id.btnSendData); btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String firstname = edFirstName.getText().toString(); String lastname = edLastName.getText().toString(); int age = Integer.parseInt(edAge.getText().toString()); User user = new User(firstname,lastname,age); Intent intent = new Intent(MainActivity.this,UserActivity.class); intent.putExtra("user",user); startActivity(intent); } }); } } |
In the receiving activity, we can retrieve the Parcelable object from the intent by using the ‘getParcelableExtra’ method, passing the same key ‘user’. This allows us to access and utilize the transferred data within the receiving activity.
That’s how we use Parcelable to transfer data between activities or fragments in our Android application. Feel free to explore further and implement Parcelable for efficient data transfer in your own projects.
Step 5: Designing the User Activity
Once we have designed our user interface, let’s take a look at the layout file. We have used the ConstraintLayout as the root element and added three TextViews: textViewFirstName, textViewLastName, and textViewAge. These TextViews will display the corresponding data we receive through Parcelable.
With the help of constraints, we have positioned the TextViews on the screen, ensuring proper alignment. The layout_width and layout_height attributes are set to ‘wrap_content’ to adjust the size based on the content.
acitivity_user.xml

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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=".UserActivity"> <TextView android:id="@+id/textViewFirstName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:text="TextView" android:textSize="34sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.501" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textViewLastName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="TextView" android:textSize="34sp" app:layout_constraintEnd_toEndOf="@+id/textViewFirstName" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="@+id/textViewFirstName" app:layout_constraintTop_toBottomOf="@+id/textViewFirstName" /> <TextView android:id="@+id/textViewAge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="TextView" android:textSize="34sp" app:layout_constraintEnd_toEndOf="@+id/textViewLastName" app:layout_constraintStart_toStartOf="@+id/textViewLastName" app:layout_constraintTop_toBottomOf="@+id/textViewLastName" /> </androidx.constraintlayout.widget.ConstraintLayout> |
In just a few simple steps, we have created a visually appealing layout for our UserActivity. Now, let’s move forward and handle the data received from the previous activity.
That’s all there is to it! Parcelable is a powerful tool in Android development that helps in efficient data transfer between components. Now, let’s see how we can retrieve and display the transferred data in our UserActivity.
Step 5: Unpacking Data in User Activity
In our code, we have defined TextView objects: tvFirstName, tvLastName, and tvAge. We retrieve references to these TextViews using the ‘findViewById’ method.
Next, we obtain the intent that started the UserActivity using the ‘getIntent’ method. We then retrieve the Parcelable object, ‘User’, from the intent using the ‘getParcelableExtra’ method, specifying the key ‘user’.
To display the data, we use the setText method on each TextView, setting the values from the User object. ‘tvFirstName’ displays the first name, ‘tvLastName’ displays the last name, and ‘tvAge’ displays the age.
With these simple steps, we have successfully retrieved the Parcelable data and displayed it in our UserActivity.
UserActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class UserActivity extends AppCompatActivity { TextView tvFirstName,tvLastName,tvAge; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user); tvFirstName = findViewById(R.id.textViewFirstName); tvLastName = findViewById(R.id.textViewLastName); tvAge = findViewById(R.id.textViewAge); Intent intent = getIntent(); User user = intent.getParcelableExtra("user"); tvFirstName.setText(user.getFirstname()); tvLastName.setText(user.getLastname()); tvAge.setText(String.valueOf(user.getAge())); } } |
That concludes our tutorial on using Parcelable for efficient data transfer. I hope you found this video informative and helpful. If you have any questions or suggestions, please feel free to leave them in the comments section below. Thank you for watching, and don’t forget to like and subscribe for more Android development tutorials!”
Feel free to make any adjustments or modifications to the script as per your preference.