Saturday, October 24, 2009

Creating SubActivity, Passing data to SubActivity and Retrieving data from SubActivity

In this tutorial, I am going to explain about the subactivity in Android.

The basic thing that I am going to explain in this tutorial is,

From a main activity calling another activity or subActivity, passing data to the subActivity using startActivityForResult() and retrieving information from the subActivity using the callback function called onActivityResult().

The UI contains TextView, EditText and a button. Whatever we type in the editText will appear in the next activity.

First we will pass data from main Activity to subActivity using Bundle. We can put all primitive data types and String to bundle. Bundle will take data as Key/Value pair. Then we will put this bundle into the Intent using intent.putExtras(bundle). We will pass this intent object to startActivityForResult() with a REQUEST_CODE.

In subActivity, we will retreive the data using

Bundle bundle = this.getIntent().getExtras();

Then by passing the desired key, we will get the values using get method.

Seems so easy, right ??

The final part is retrieving information from subActivity :: This is done through the method called setResult()

Same like in mainActivity, whatever we want to retrieve, will put in bundle, then in intent. Pass this intent to setResult() with a RESULT_CODE. In mainActivity we will receive the information in onActivityResult().

The source code of the program is,

Code for mainActivity,

public class PassingData extends Activity {

EditText text;
Button next;
String name;
TextView statText, retData;
static int REQUEST_CODE = 1;

public void onCreate(Bundle savedInstanceState) {

statText = (TextView) findViewById(;
retData = (TextView) findViewById(;

text = (EditText) findViewById(;
next = (Button) findViewById(;
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
name = text.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("name", name);
Intent intent = new Intent(PassingData.this, RetrievePage.class);
startActivityForResult(intent, REQUEST_CODE);

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("Req code : "+requestCode, "Req code : "+requestCode);
Log.i("Result code : "+resultCode, "Result code : "+resultCode);

Code for subActivity,

public class RetrievePage extends Activity {

String retrievedData;
TextView viewData;
static int RESULTCODE = 2;
public void onCreate(Bundle savedInstanceState) {
Bundle bundle = this.getIntent().getExtras();
if(bundle != null) {
retrievedData = bundle.getString("name");
viewData = (TextView) findViewById(;
Bundle b = new Bundle();
b.putString("Value", retrievedData);
Intent intent = new Intent();
setResult(RESULTCODE, intent);

Friday, October 23, 2009

Blinking effect on an Image

In this tutorial, I am going to explain blinking an image using Handler. Actually, this is an extension of SplashScreen.

In nutshell, what this tutorial is going to explain is,
An Image will be visible for one second and for the next second, it will be invisible. I am doing this using two handlers. One for making the image visible and another for making the image invisible.

First in onCreate() method, I am calling the first handler called "splashHandler" using sendMessageDelayed() method. I am passing an object of Message and variable of data type "long", its for how long the image has to invisible. Then from "splashHandler", I am calling another handler called "resumeHandler" for making the image visible. In this also, I am passing an object of Message and a variable of long type to the sendMessageDelayed() function.

Then from "resumeHandler", again calling "splashHandler with the message object and a variable of long type. So this will run continously and have a blinking effect on the image.

The source code is,

public class SplashScreen extends Activity {

ImageView img;
private final int STOPSPLASH = 0;
private final int RESUMESPLASH = 1;
private final int SPLASHTIME = 1000;
private Message stopMessage, resumeMessage;

private Handler splashHandler = new Handler() {

public void handleMessage(Message msg) {
switch(msg.what) {
resumeMessage = new Message();
resumeMessage.what = RESUMESPLASH;
resumeHandler.sendMessageDelayed(resumeMessage, SPLASHTIME);

private Handler resumeHandler = new Handler() {

public void handleMessage(Message msg) {
switch(msg.what) {
stopMessage.what = STOPSPLASH;
splashHandler.sendMessageDelayed(stopMessage, SPLASHTIME);
public void onCreate(Bundle savedInstanceState) {
img = (ImageView) findViewById(;
stopMessage = new Message();
stopMessage.what = STOPSPLASH;
splashHandler.sendMessageDelayed(stopMessage, SPLASHTIME);


I am just taken an image and explaining the flow of the application. The screen shot is just putting for your reference,

For one second, the image will be visible and in the next second, will disappear, so, will have a blinking effect on the image.

Thursday, October 22, 2009

Setting Wallpaper

In this tutorial, I am going to explain, how to set wallpaper through android application. The code for this is,

Intent intent = new Intent();

We have to give permission also in Manifest file as

uses-permission android:name="android.permission.SET_WALLPAPER/>

The screen-shot for the above program will be as,

Calling a Number

In this tutorial, I am going to explain, how to call to a number through android application. The code for that is,

Intent intent = new Intent();

The code is self explanatory, nothing big in it. Then, in the Manifest file we have to give permission too.

<uses-permission android:name="android.permission.CALL_PHONE"/>

The screen-shot of the program - Emulator 5554 calling emulator 5556 in this application as,

Sunday, October 4, 2009

Sending SMS in Android

In this tutorial I am going to explain sending text message from one android mobile to another.

The UI of our application contains just two Editexts, one for sender's number and another for the message body. Then one "SEND" button.

I am going to demonstrate this by opening two different emulators(5554 & 5556). The phone number is the number that we see on top left side of the emulator, ie, here, 5554 & 5556 respectively.

Launch the SMS application on both emulators and start SMSing. You will get the notifcation on top of other emulator.

Now, how to implement the SMS Application. The main part of the application is the method sendSMS() and it takes two parameters, Sender's number and the content of the SMS.

sendSMS() contains, one object of PendingIntent and a BroadCastReceiver to send SMS.
PendingIntent contains an Intent and a target action and the return object can be handed on a different application, so that, the action that we described can be performed at a later stage. Then, the BroadcastReceiver to broadcast our SMS, textmessage.

The sourcecode for sendSMS() is,

public void sendSms(String phoneNumber, String message) {
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);
// after SMS sending
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.makeText(getBaseContext(), "Generic failure",
Toast.makeText(getBaseContext(), "No service",
Toast.makeText(getBaseContext(), "Null PDU",
Toast.makeText(getBaseContext(), "Radio off",

}, new IntentFilter(SENT));

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, sentPI,

We need to do one last thing, we have to put permission in Manifest file for sending and receiving SMS as,

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

In this, emulator, 5554, sending a message to 5556.

and, emulator 5556, replying to 5554 as,

Friday, October 2, 2009

Rotating a bitmap in Android

Here, we have an image of globe and I am going to rotate it.

In Android, we can rotate a bitmap in canvas. Its simple, but we have to know the save() and restore() methods in Canvas class.

We can load a bitmap using

Bitmap globe = BitmapFactory.decodeResource(context.getResources(), R.drawable.globe);

in onDraw() method,

we can rotate the bitmap using

canvas.roatate() method. But here the problem is, the whole canvas will rotate. But by using, save() and restore() we can solve this problem.

Before rotating the, just call then, call

then, draw the image. After drawing the image, just call canvas.restore().

This will solve the problem of rotating the whole canvas.

To show, canvas is restored, I am drawing a text after drawing bitmap in canvas by,
canvas.drawText("Here our globe is rotating", 50, 400, textColor);
In the end of onDraw, just call invalidate();

Now, our globe will rotate.

and the text under the globe is static.

This will do.

If you have any queries, about my post, feel free to contact @

Happy to help You....