I dream a dream.

Archive for March, 2011

Create a new intent in android.

What is android intent?

Android folds multiple ideas into the concept of an intent. You can use intents to invoke other applications from your application. You can use intents to invoke internal or external components from your application. You can use intents to raise events so that others can respond in a manner similar to a publish-and-subscribe model. You can use intents to represent actions.
At the simplest level, an intent is an action that you can tell Android to invoke. The action Android invokes depends on what is registered for that action. (Pro Android – Apress)

Register an activity as android intent?

In this section, I want to describe a way to register an activity as an android intent.

If you don’t know how to add an activity, please read my post here. Suppose that you created an activity name MyActivity. Now, open AndroidManifest.xml file, choose the tab AndroidManifest.xml to edit it manually.  Add action and category element inside your activity element as below image.

<activity android:name=”MyActivity”>
                <action android:name=”sandbox.basic.activity.intent.action.show_my_activity”></action>
                <category android:name=”android.intent.category.DEFAULT”></category>

The general convention for an action name is <your-package-name>.intent.action.YOUR ACTION NAME

From now, whenever you need to use your intent, you can invoked it such as below:

String actionName = “sandbox.basic.activity.intent.action.ShowMyActivity”;
Intent newIntent = new Intent(actionName);



Declare new activity in android

There are some ways to declare a new activity in android. But in this section, I just want to describe an simplest way to do it.

– Open the AndroidManifest.xml file then follow below steps to add new activity.


– Look at the Attributes for Activity section at right side, click Name to create a new class or Browse to choose an existing class. This class will contain code-behind of this activity and it must inherit from Activity class under android.app.Activity namespace. Also you can configure other properties for you activity such as orientation, theme, label…


– Right click on Layout folder, choose new > Android XML File. In the new Android XML File dialog, type the class name then hit button Finish. Note that the layout file name must end with “.xml” and just contains a-z, 0-9 character.


– Open MyActivity.java file, edit the OnCreate method as below image.


Now, whenever MyActivity is invoked, it load the myactivity.xml layout to the screen.


The End.