Hello all
This is a simple post that shows How to pass an Object from One Activity to another in Android.
I am going to pass the following object from One Activity to another.
MyObject{
String name;
String website;
}
This is the class that defines the Object.
package com.coderzheaven.passarraylist;
import android.os.Parcel;
import android.os.Parcelable;
public class MyObject implements Parcelable{
String name,website;
public MyObject(){
}
private MyObject(Parcel in){
this.name = in.readString();
this.website = in.readString();
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeString(this.website);
}
public static final MyObject.Creator<MyObject> CREATOR = new MyObject.Creator<MyObject>() {
public MyObject createFromParcel(Parcel in) {
return new MyObject(in);
}
public MyObject[] newArray(int size) {
return new MyObject[size];
}
};
}
This is the first activity that sends the object to second activity.
package com.coderzheaven.passarraylist;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setTitle("Main Activity");
final MyObject myobj = new MyObject();
myobj.setName("Coderz");
myobj.setWebsite("CoderzHeaven.com");
TextView name = (TextView)findViewById(R.id.name);
name.setText("Name : " + myobj.getName());
TextView website = (TextView)findViewById(R.id.website);
website.setText("Website : " + myobj.getWebsite());
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("myobject", myobj);
startActivity(intent);
}
});
}
}
This is the second Activity.
package com.coderzheaven.passarraylist;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
this.setTitle("Second Activity");
Bundle b = getIntent().getExtras();
if(b!=null){
MyObject myobj = (MyObject)getIntent().getExtras().getParcelable("myobject");
TextView name = (TextView)findViewById(R.id.name);
name.setText("Name : " + myobj.getName());
TextView website = (TextView)findViewById(R.id.website);
website.setText("Website : " + myobj.getWebsite());
}
}
}
activity_main.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textStyle="bold" />
<TextView
android:id="@+id/website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_margin="5dp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/website"
android:text="Pass Object to Next Activity" />
</RelativeLayout>
second_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textStyle="bold" />
<TextView
android:id="@+id/website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_margin="5dp"
android:textStyle="bold" />
</RelativeLayout>


You can download the tutorial PDF from here.
PLease leave your comments.
Hello everyone…
In this post I will show you how to listen to hardware button in an android smartphone.
This post especially shows how to listen to the home button in android. But my advice is never override the home button in your app.
Home button is to move the currently running process to background. But you can override the Backbutton and other buttons, android allows that on the keyDown Method. But if you try to match the “Home” button in the keyDown method it will not work. For that we have to follow another way which is shown in this post.
This simple java code handles the button pressed.
package test.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
public class Test2Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
System.out.println("KEYCODE_HOME");
showDialog("'HOME'");
return true;
}
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
System.out.println("KEYCODE_BACK");
showDialog("'BACK'");
return true;
}
if ((keyCode == KeyEvent.KEYCODE_MENU)) {
System.out.println("KEYCODE_MENU");
showDialog("'MENU'");
return true;
}
return false;
}
void showDialog(String the_key){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You have pressed the " + the_key + " button. Would you like to exit the app?")
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
finish();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.setTitle("CoderzHeaven.");
alert.show();
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
public void onUserLeaveHint() { // this only executes when Home is selected.
// do stuff
super.onUserLeaveHint();
System.out.println("HOMEEEEEEEEE");
}
}
When “onUserLeaveHint()” function is defined you can actually listen to the home button press and do some saving of data or something.
public void onUserLeaveHint() { // this only executes when Home is selected.
// do stuff
super.onUserLeaveHint();
System.out.println("HOMEEEEEEEEE");
}



Please leave your comments on this post and share it on the social Networks.
1. Support for Mobile Payments
Android phones which have android gingerbread option will help you pay all your bills through your mobile phone. No need to wait in line or switch on your computer to pay bills. Sit in your office and pay your bills using android OS gingerbread.
2. One Touch Copy and Paste
Using gingerbread android OS you can just press and hold the text to be copied from any website or text messages to copy and paste them in different location. You can even select the boundary line for the text copied.
3. Internet Calling
Users using android OS gingerbread will be able to do internet calling through mobile phones. One touch through your mobile phone will help you have a voice call also. Anyways Google will have its own copy for the contact list you dial.
4. Fastest and Best Battery Back Up
Google promises that android gingerbread is the fastest OS in android phones. Smart phones that have all high usage options will surely have a poor battery backup. Android OS gingerbread stops the battery consumption over a point of time and helps to have a better battery backup.
5. Android Market
Any application you need to download or install in your android phone, you can just login to your online android market. Online downloads will directly get installed in your mobile phone through the customized Gmail id.
Hello all….
In a previous post I showed a method to share global variables across your android application.
Those were done using a static class.
Now today I will show you another way of sharing global variables across the application
Check these posts for sharing data across the application.
1. http://coderzheaven.com/2011/05/global-variables-in-android/
2. http://coderzheaven.com/2011/05/global-variables-in-android%e2%80%a6/
3. http://coderzheaven.com/2011/03/passing-data-between-intents-in-android/
Now in this method. first create a class named “GlobalClass” and extends the “Applictation” class.
This is how it will look like.
package com.coderzheaven.pack;
import android.app.Application;
class GlobalClass extends Application {
public static String myVal;
}
Inside which I have a variable named “myVal” which is a string. This is the global string variable that I am using to pass values across the application.
Now the main java class
Here I am using two activities just to demonstrate the sharing of global variables across the application.
Here is the first activity.
package com.coderzheaven.pack;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GlobalVariablesDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Assigning some value to the global variable //
GlobalClass.myVal = "Hello from CoderzHeaven";
Button b = (Button)findViewById(R.id.Button01);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(GlobalVariablesDemo.this, SecondClass.class));
}
});
}
}
This line shows how to assign value to a global variable.
GlobalClass.myVal = “Hello from CoderzHeaven”;
Now the second activtiy.
package com.coderzheaven.pack;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondClass extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
System.out.println("Accessing the global string : " + GlobalClass.myVal);
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText("Global String : " + GlobalClass.myVal);
}
}
This is how you access the global variable.
GlobalClass.myVal
That’s all done.
Now go on and run the application.


Please leave your comments if you like this post.
Hi,
Given below is a simple concatenation example in JAVA where two strings are joined together to form a single string.
public class MainClass {
public static void main(String[] arg) {
String yourString = "Coderz" + "Heaven";
System.out.println(yourString);
}
}
And…the output will be.. “CoderzHeaven”
Hi,
For printing statements in PHP we make use of echo like this.
<?php echo "Coderz Heaven"; ?>
An alternative way of echo or a shorthand of echo will look like this.
<?="Coderz Heaven"?>
Hi,
Want to try out a simple hide and show toggle using jQuery? The example given below shows how you can simply hide and show a paragraph in html using jQuery. Try it out!
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#hideMe").click(function(){
$("p").hide();
});
$("#showMe").click(function(){
$("p").show();
});
});
</script>
</head>
<body>
<p>Coderz Heaven! Click 'HideMe' for hiding! Click 'ShowMe' for showing</p>
<button id="hideMe">HideMe</button>
<button id="showMe">ShowMe</button>
</body>
</html>
Hi,
Want to know how to replace a string in C#? See the following code.
using System;
class MainClass {
public static void Main() {
string testStr= "Coderz";
testStr= testStr.Replace("erz", "ings");
Console.WriteLine(testStr);
}
}
Guess output?
“Codings”