Archive

Archive for the ‘android java’ Category

Preference / Settings

June 26th, 2009 No comments

(Android phone – how-to/example)

An android.preference.PreferenceActivity “shows a hierarchy of Preference objects as lists, possibly spanning multiple screens. These preferences will automatically save to SharedPreferences as the user interacts with them. Furthermore, the preferences shown will follow the visual style of system preferences. It is easy to create a hierarchy of preferences (that can be shown on multiple screens) via XML. For these reasons, it is recommended to use this activity (as a superclass) to deal with preferences in applications.” For example:

public final class PreferencesActivity extends android.preference.PreferenceActivity
implements OnSharedPreferenceChangeListener
{

In order to display the preferences the PreferenceActivity may call addPreferencesFromResource(int preferencesResId) method where preferencesResId is the id of preference.xml.
A preference.xml may look like this:

< PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
< PreferenceCategory android:title="First category">
< ListPreference
android:key="preferences_set_1"
android:title="First SET 1"/>
< EditTextPreference
android:key="preferences_set_2"
android:title="First SET 2"/>
< CheckBoxPreference
android:key="preferences_set_3"
android:defaultValue="true"
android:title="First SET 3"/>
< /PreferenceCategory>

< PreferenceCategory android:title="Second category">
< CheckBoxPreference
android:key="preferences_set_4"
android:defaultValue="false"
android:title="Second Set 4"/>
< Preference
android:key="preferences_set_5"
android:title="Second Set 5"/>
< /PreferenceCategory>

< /PreferenceScreen>

Note: The full documentation for PreferenceCategory, EditTextPreference, CheckBoxPreference, Preference may be found on http://developer.android.com/reference/android/preference/PreferenceActivity.html

Every preference must have a key defined in XML and in code. Example:

public static final String KEY_SET_1 = “preferences_set_1″;

To link the XML to the java code you need to do the folowing steps:
1. Create a PreferenceScreen object.“android.preference.PreferenceScreen represents a top-level Preference that is the root of a Preference hierarchy. A PreferenceActivity points to an instance of this class to show the preferences.”
Example: PreferenceScreen preferences;

2. Initialize PreferenceScreen object using getPreferenceScreen method. “android.preference.PreferenceActivity.getPreferenceScreen() method gets the root of the preference hierarchy that this activity is showing.”
Example: preferences = getPreferenceScreen();

3. Create a preference object that is also in XML and use findPreference(CharSequence key) method where key is the key of the preference. “android.preference.PreferenceGroup.findPreference(CharSequence key) finds a Preference based on its key.”
Example: CheckBoxPreference m_cbpSET1 = (CheckBoxPreference) preferences.findPreference(KEY_SET_1);

If you want to add an action when the preference is changed you must use the setOnPreferenceChangeListener method. This method “sets the callback to be invoked when this Preference is changed by the user (but before the internal state has been updated).”
Example:

m_cbpSET1.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
/* your code here */
return true;
}
});

Code example: you may find useful to see the whole implementation of this in the Mezzofanti application – google code. The code is released under Apache License, ver 2.0, so you can freely use it free of charge in your own code. Consult the class PreferenceActivity.java.

Using the layouts

June 10th, 2009 No comments

(Android phone – how-to/example)

If you just start coding for Android, you will find it a bit tricky to do your own design. For a visual basic-designer, you can consider DroidDraw

Before reading further, we recommend to browse through the official docs. Below are just a few examples and explanations.

LinearLayout example:
LinearLayout aligns all children in a single direction — vertically or horizontally, depending on how you define the orientation attribute. All children are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding).

For example, if you want to create a double-lined status bar, you should do the following:

< !-- Status text frame -->
< LinearLayout android:id="@+id/status_view"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@color/status_view"
android:baselineAligned="false"
android:padding="4px">

< !-- Status text 1 -->
< TextView android:id="@+id/status_text_view1"
android:layout_width="wrap_content"
android:layout_height="20px"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"
android:text="@string/msg_default_status1"
android:textColor="@color/status_text1"
android.textSize="14sp"/>

< !-- Status text 2 -->
< TextView android:id="@+id/status_text_view2"
android:layout_width="wrap_content"
android:layout_height="20px"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"
android:text="@string/msg_default_status2"
android:textColor="@color/status_text2"
android.textSize="14sp"/>

< /LinearLayout>

FrameLayout example:
FrameLayout is the simplest type of layout object. It’s basically a blank space on your screen that you can later fill with a single object — for example, a picture that you’ll swap in and out.
For example, if you want to create a double-lined status bar, with some level of opacity, you should include the LinearLayout defined above in the body of the following FrameLayout:

< FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@color/transparent"/>

< !-- INSERT LinearLayout here -->

< /FrameLayout>

RelativeLayout example:
RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. Elements are rendered in the order given, so if the first element is centered in the screen, other elements aligning themselves to that element will be aligned relative to screen center.

For example, if you need to create a text-editor with a status bar at the bottom, it is better to consider the RelativeLayout because:
- you do not know the height/width of the screen (you may know them, but you want to have a portable design between different phones)
- you do know the height of the status bar, so you can start drawing from the bottom, and keep the edit-text relative to this status-layout.

< RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">

< !-- Status layout -->
< LinearLayout
android:id="@+id/status_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="20px"
android:layout_alignParentBottom="true">

< TextView
android:id="@+id/status_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/status_text">
< /TextView>
< /LinearLayout>

< !-- Edit text layout -->
< LinearLayout
android:id="@+id/edit_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/status_layout">

< EditText
android:id="@+id/edit_text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:editable="false"
android:visibility="visible"
android:cursorVisible="false"
android:gravity="top">
< /EditText>

< /LinearLayout>

< /RelativeLayout>

Code example: you may find useful to see an implementation of this in the Mezzofanti application – google code. The code is released under Apache License, ver 2.0, so you can freely use it free of charge in your own code. Consult the /layout directory.

Categories: android java Tags:

Using the Camera

April 15th, 2009 No comments

(Android phone – how-to/example)

In order to use the Android Camera you need your activity to implement android.view.SurfaceHolder.Callback, example:

public class CameraTest extends Activity implements SurfaceHolder.Callback
{
// Camera object
private Camera m_oCamera;

In your Activity, you’ll also need:
- an android.view.SurfaceView.SurfaceView to display the picture preview and
- an android.view.SurfaceHolder that is an <<abstract interface to someone holding a display surface. Allows you to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface.>>

private SurfaceView m_oSurfaceView;
private SurfaceHolder m_oSurfaceHolder;

Than you initialize them:
m_oSurfaceView = new SurfaceView(this);
setContentView(m_oSurfaceView);

Connect SurfaceHolder to SurfaceView
mSurfaceHolder = mSurfaceView.getHolder();

After that, you will need to add a callback to the surface holder. The callback will inform you when the surface was created/changed/destroyed:
mSurfaceHolder.addCallback(this);

Set surface not to have its own buffers in order to immediately display the image
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

Also you may want to implement surfaceCreated(SurfaceHolder holder). This method is called when surface is created, for example to initialize the camera object:

public void surfaceCreated(SurfaceHolder holder)
{
m_oCamera = Camera.open();
}

The method surfaceChanged(SurfaceHolder holder, int format, int width, int height) is called after surface is created. Here you can start to preview and set camera/preview parameters.

public void surfaceChanged(SurfaceHolder holder,
int format,
int width,
int heigth)
{
/* if the preview is already running (for example the application was paused) */
if (m_bPreviewRunning)
m_oCamera.stopPreview();

/* set preview parameters */
Camera.Parameters params = m_oCamera.getParameters();
params.setPreviewSize(width, heigth);
m_oCamera.setParameters(params);

try
{
/* Sets the SurfaceHolder to be used for a picture preview. */
m_oCamera.setPreviewDisplay(holder);
}
catch (IOException e) {}

/* start camera preview */
m_oCamera.startPreview();
m_bPreviewRunning = true;
}

The surfaceDestroyed(SurfaceHolder holder) method is called immediately before a surface is being destroyed. You must stop camera preview because the surface will no longer exist.

public void surfaceDestroyed(SurfaceHolder holder)
{
m_oCamera.stopPreview();
m_bPreviewRunning = false;
m_oCamera.release();
}

When you want to take a picture you will call:

m_oCamera.takePicture(null, null, m_cbPicture);

This will take a picture and after the picture is jpeg compressed the callback m_cbPicture will be called.
NOTE: In Android SDK 1.5 the raw picture callback has a bug and image buffer is empty.

This is how your jpeg callback should look like.
Camera.PictureCallback m_cbPicture = new Camera.PictureCallback()
{
public void onPictureTaken(byte[] data, Camera c)
{
/* do something with picture from data array
do something with picture from data array
Hint: use the Bitmap class, but do not do much processing in the callback, send yourself a message with the message handler, and do the processing at some other point (maybe in a different thread if you will implement lots of processing)
*/

/* restart camera preview */
m_oCamera.startPreview();
}
};

Code example: you may find useful to see an implementation of this in the Mezzofanti application – google code. The code is released under Apache License, ver 2.0, so you can freely use it free of charge in your own code. Consult the class CameraManager.java.

Categories: android java Tags:

Obtain root access on your Android HTC Dream phone

March 25th, 2009 No comments

(Android phone – how-to)

Step 1 – Preparing to Get Root on your Phone
Getting root on your phone involves exploiting a security hole that existed in an early version of Android. The versions with the security holes are known as RC19, RC28, and RC29 (or RC7 or lower for UK phones). As of RC30 (RC8 on UK), Google patched the security hole so that your phone can not be rooted (don’t worry, we can still root it!).
To check your version, go to the Home Screen, click your menu button, choose “Settings”, and then click “About phone”.
If you have US-RC30/UK-RC8 or higher, you will first need to downgrade your phone to a previous version. (Skip these steps otherwise)

1. Format your phone’s SD card to FAT32 mode:
Hook your phone up to your computer using a USB cable and then wait for the notification to show up in your title bar of your phone.
Click the notification, and then click “Mount”.
A new removable disk should show up on your computer. Right click it and select Format, and select FAT32 as the file system type.
Note: Do not format the sdcard directly inserting it in your computer. You may find that formatting FAT32 with your computer not to work. This may sound silly, but there may be small differences (that matter) between the FAT32 formats. In this case, do format the sdcard with another phone.

2. Download and unzip the RC29 or RC7 image file. Copy the DREAMIMG.nbh file to the SD card. (RC29 for US, RC7 is for UK)
3. Turn the device power off.
4. Hold Camera button, and press Power button to entry bootloader mode. You should see a gray/white screen with instructions to flash your phone with the update on your SD card. If you don’t see that, make sure you followed the instructions properly.
5. As per the on-screen instructions, press the Power button to start upgrade procedure. DO NOT DO ANYTHING TO INTERRUPT THIS PROCESS.
6. After it is finished, perform the restart your phone.

Step 2 – Rooting your RC29 or lower phone:
On RC29 phones and lower, anything you type into your keyboard is also being run in a hidden console with root permissions. More information regarding that at the bottom of this post. But, to get root access, do the following:

Instructions:
1. Download recovery.img and copy it to your SD card (see the previous instructions on how to copy from your computer to your Phone’s SD card).
2. Download the Hard SPL and copy the zip file to the SD card.
3. All files must be on the root of your SD card.
4. Restart your phone. Wait for your phone to start up fully and show the home screen.
5. After your phone starts up, hit the enter key twice, type “telnetd” and press enter. (Yes, it will start up a contact search, don’t worry. Just type it.)
6. Download an Android “Telnet” application from the Market and connect to localhost.
7. If you connect successfully, you will have a root prompt “#”.
8. Type the following into Telnet (these commands will give you root access easier in the future):
mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system
cd sdcard
flash_image recovery recovery.img
cat recovery.img > /system/recovery.img
Now you have root!

Step 3 – What To Do Now That You Have Root
Now that you have root, you will want to apply “Hard SPL” to your phone. HardSPL is what will allow you to apply flash images from other regions (like UK on US phones, and vice versa), create full backups of your phone, install the latest build from the Android source, and usually resurrect your phone if it is “bricked”. You have already downloaded the file to your SD card, so now you can apply it.
1. Power off your phone.
2. Start up in recovery mode by holding home and pressing power.
3. You will now enter recovery mode. You should see an exclamation.
4. If you do not see a menu on screen, press Alt-L to show the menu.
5. Press Alt-S to apply the update from the SD card.
6. After the update is complete, hold Home and press Back to restart.

And now, the last step! You are still running an old version of Android, but you want to upgrade to the latest and greatest update! You can do this, and not lose root by downloading modified versions of the updates.

First, choose a modified image from the list below:

ADP1.1: (md5: bacc58302e0b239d66c7bcc8db6c434b)
http://jf.odiness.com/v1.41/JFv1.41_…ronment.tar.gz
http://android-dls.com/forum/index.p…rb_v=viewtopic
http://andblogs.net/2009/01/jesusfre…mages-are-out/

RC33: (md5: f24b6c237775147cb4bc42efc2393973)
http://jf.odiness.com/v1.41/JFv1.41_…ronment.tar.gz
http://android-dls.com/forum/index.p…rb_v=viewtopic
http://andblogs.net/2009/02/new-rc33…om-jesusfreke/

RC8: (md5: de2d0d34adbb4015ee3aa5e4e7ca3c07)
http://jf.odiness.com/v1.41/JFv1.41_…ronment.tar.gz
http://android-dls.com/forum/index.p…rb_v=viewtopic
http://andblogs.net/2009/01/jesusfre…mages-are-out/

1. Download the image.
2. Rename it to update.zip.
3. Copy it to your phone’s SD card.
4. Turn your phone off.
5. Start up in recovery mode by holding Home and pressing Power.
6. Press alt-W to wipe your device. (You need to do this, or the device may hang at the flashing Android screen)
7. Press alt-S to apply the update.

Note: the original message was posted here.

Some other interesting links you may consider:
1. You can install a full Linux distribution, like Debian.
2. You can install the latest Android build directly from the Android source tree.
3. Customize your boot image.
4. Create custom ROMs.
5. How to go back to official cupcake 1.5 dev.
6. Video of the steps above