How to create a nested parcelable object in Android

In this android programming source code example, we are going to create a nested parcelable object in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to create a nested parcelable object in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical" 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=".json.Json17">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Parcelable Nested Objects"
        android:layout_gravity="center_horizontal"
        android:textSize="16sp"
        android:paddingBottom="16dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal"/>

</LinearLayout>

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.inducesmile.inducesmile_button.R;

public class Json17 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json17);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Json17.this, JsonActivity.class);
                //ContactInfo Class
                //ContactInfo contactInfo = new ContactInfo();
                ContactInfo.Address address = new ContactInfo.Address("Lagos", "Nigeria");
                intent.putExtra("key", address);
                startActivity(intent);
            }
        });
    }
}

ContactInfo.java

import android.os.Parcel;
import android.os.Parcelable;

import java.util.Date;

public class ContactInfo implements Parcelable {
    public static final Creator<ContactInfo> CREATOR = new Creator<ContactInfo>() {
        @Override
        public ContactInfo createFromParcel(Parcel in) {
            return new ContactInfo(in);
        }

        @Override
        public ContactInfo[] newArray(int size) {
            return new ContactInfo[size];
        }
    };
    private String name;
    private String surname;
    private int id;
    private Address address;

    ;

    public ContactInfo() {
    }

    protected ContactInfo(Parcel in) {
        name = in.readString();
        surname = in.readString();
        id = in.readInt();

        address = in.readParcelable(Address.class.getClassLoader());
    }

    @Override
    public String toString() {
        return "ContactInfo{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", surname='" + surname + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(surname);
        dest.writeInt(id);

        //Writing the nested class to parcel
        dest.writeParcelable(address, flags);
    }

    //Nested Class
    public static class Address implements Parcelable {


        public static final Creator<Address> CREATOR = new Creator<Address>() {
            @Override
            public Address createFromParcel(Parcel in) {
                return new Address(in);
            }

            @Override
            public Address[] newArray(int size) {
                return new Address[size];
            }
        };
        private String city;
        private String country;
        private Date date;

        protected Address(Parcel in) {
            city = in.readString();
            country = in.readString();
            date = new Date(in.readLong());
        }

        public Address(String city, String country) {
            this.city = city;
            this.country = country;
        }

        public Address(String city, String country, Date date) {
            this.city = city;
            this.country = country;
            this.date = date;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public Date getDate() {
            return date;
        }

        public void setDate(Date date) {
            this.date = date;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(city);
            dest.writeString(country);
            dest.writeLong(date.getTime());
        }

        @Override
        public String toString() {
            return "Address{" +
                    "city='" + city + '\'' +
                    ", Country='" + country + '\'' +
                    ", Date='" + date + '\'' +
                    '}';
        }

    }
}
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.inducesmile.inducesmile_button.R;

public class JsonActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json_activity);

        Bundle bundle = getIntent().getExtras();
        JavaObjects javaObjects = bundle.getParcelable("Key");

        TextView textView = findViewById(R.id.text);
        TextView textView2 = findViewById(R.id.text2);
        TextView textView3 = findViewById(R.id.text3);
        //textView.setText(javaObjects.toString());

        //Nested Object
        ContactInfo.Address contactInfo = getIntent().getParcelableExtra("key");
        //textView2.setText(contactInfo.toString());

        //Nested Object with Date
        ContactInfo.Address date = getIntent().getParcelableExtra("date");
        textView3.setText(date.toString());
    }

}

activity_json_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Receiving Objects from Another Activity"
        android:textSize="16sp"
        android:paddingBottom="16dp"
        android:layout_gravity="center_horizontal"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="@color/colorAccent"
        android:id="@+id/text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="@color/colorAccent"
        android:id="@+id/text2"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="@color/colorAccent"
        android:id="@+id/text3"/>

</LinearLayout>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment