I dream a dream.

Archive for the ‘Android’ Category

Differents among px, dp, and sp unit in android

Pixels – corresponds to actual pixels on the screen.

Density-independent Pixels – an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion.

Scale-independent Pixels – this is like the dp unit, but it is also scaled by the user’s font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user’s preference.

In my experience, never use px in android xml layout file (except special case). You should use dp unit instead. Since sp unit is preferred to the font size which was set on real device by user, you should use this measurement when defining text size. But, never use it for layout size.


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.