Pass Custom Object Using Parcelable Android

Car.java
package rz.rasel.objectsendfromactivity;

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

public class Car implements Parcelable {

private String CarName;
private String companyName;
private int contactNum;
public String getCarName() {
return CarName;
}
public void setCarName(String CarName) {
this.CarName = CarName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public int getContactNum() {
return contactNum;
}
public void setcontactNum(int contactNum) {
this.contactNum = contactNum;
}
public static final Parcelable.Creator CREATOR =new Creator() {

@Override
public Car createFromParcel(Parcel source) {
// TODO Auto-generated method stub
Car mCar=new Car();
mCar.CarName=source.readString();
mCar.companyName=source.readString();
mCar.contactNum=source.readInt();
return mCar;
}

@Override
public Car[] newArray(int size) {
// TODO Auto-generated method stub
return new Car[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}

@Override
public void writeToParcel(Parcel parcel, int flags) {
// TODO Auto-generated method stub
parcel.writeString(CarName);
parcel.writeString(companyName);
parcel.writeInt(contactNum);
}

}


main_activity.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:gravity=”center_vertical”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:gravity=”center_vertical|center_horizontal|center”
android:text=”@string/button” />

</RelativeLayout>


MainActivity.java
package rz.rasel.objectsendfromactivity;

import android.os.Bundle;
import android.os.Parcelable;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener
{
public final static String PAR_KEY = “rzraselSend”;

Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = ( Button ) findViewById( R.id.button );
button.setOnClickListener( this );
}
@Override
public void onClick( View view )
{
Car mCar = new Car();
mCar.setCarName( “jaguar xf sedan” );
mCar.setCompanyName( “jaguar” );
mCar.setcontactNum( 123456789 );
try
{
Intent mIntent = new Intent( this, ReceiveObjActivity.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable( PAR_KEY, mCar );
mIntent.putExtras( mBundle );

startActivity(mIntent);
}
catch( Exception e )
{
Toast.makeText( getBaseContext(), e.toString(), 5000 ).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


receive_obj_activity.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:layout_margin=”10dp”>

<TextView
android:id=”@+id/desc”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:text=”Test”
android:textColor=”#cc0033″
android:textSize=”16dp” />

</RelativeLayout>


ReceiveObjActivity.java
package rz.rasel.objectsendfromactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;

public class ReceiveObjActivity extends Activity
{
TextView descTxt;
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.display_obj );
findViewsById();
Car mCar = (Car)getIntent().getParcelableExtra( MainActivity.PAR_KEY );
display( mCar );

}
private void findViewsById()
{
descTxt = ( TextView ) findViewById( R.id.desc );
descTxt.setText( “” );
}
private void display( Car mCar )
{
descTxt.append( Html.fromHtml( ”
Car Name :” + mCar.getCarName()
+ ”
Company Name :” + mCar.getCompanyName()
+ ”
Contact Num :” + mCar.getContactNum()
) );
}
}


AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”rz.rasel.objectsendfromactivity”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”rz.rasel.objectsendfromactivity.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”ReceiveObjActivity”></activity>
</application>

</manifest>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s