Posts

Showing posts from October, 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 ??

T…

Blinking effect on an Image

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…

Setting Wallpaper

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


Intent intent = new Intent();
intent.setAction(Intent.ACTION_SET_WALLPAPER);
startActivity(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

Image
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();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:5556"));
startActivity(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,


Sending SMS in Android

Image
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 …

Rotating a bitmap in Android

Image
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


canvas.save() then, call
cavas.rotate(degree)
degree+=5;

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 stati…