Installing assets

August 24th, 2009 No comments

(Android phone – how-to/example)

Android allows a nice feature of adding your own files in the final apk package. These files are called “assets”. For adding assets to your project you simply add your files to the “prj_name/assets” directory.

The files you add in the assets directory will be zipped in the final “apk” project, along with the executable-files and the other resources.

One has two methods to access the asset files:
a. use android.content.res.AssetManager
Note that this will limit your asset files to 1Mb, at least for Android 1.5 release.
b. use java.util.zip
This has no limitations to size, but will access all resources in the zip, so one has to filter the results.

The implementations at a glance:
a. android.content.res.AssetManager

AssetManager am = getResources().getAssets(); // get the local asset manager

InputStream is = am.open( asset ); // open the input stream for reading

while ( true ) // do the reading
{
     int count = is.read(buf, 0, MAX_BUF);
     // .. use the data as you wish
}

b. java.util.zip
Your apk file is installed in /data/app/com.name.of.package.apk
Given this location of your apk file, the unzip method is straight-forward:

ZipFile zipFile = new ZipFile(m_sZipFilename); // create the zip file

Enumeration entries = zipFile.entries(); // get the enumeration of the entries in the zip
while (entries.hasMoreElements()) // while the zip still has elements
{
     ZipEntry entry = (ZipEntry)entries.nextElement(); // get each entry at a time

     String filename = entry.getName(); // get entry name
     zipFile.getInputStream(entry); // get the input stream for the given entry – you can use this to unzip the asset
     // process the entry as you like (copy it in the
}

Some other useful methods of the ZipEntry class are:
- getSize(): Gets the uncompressed size of this ZipEntry.
- isDirectory(): Gets the uncompressed size of this ZipEntry.
- getCompressedSize(): Gets the compressed size of this ZipEntry

Categories: android java Tags: ,

Mezzofanti – Install&Run Tutorial

August 20th, 2009 4 comments

(Android phone – application, how-to)

This tutorial shows you how to run the Mezzofanti application from your Android SDK:
1. Download and install Android SDK
2. Download and install Android NDK
(optional) read our post on how to interface C/C++ with Java, with example on Tesseract, here.
3. Download Mezzofanti source code and patches

Note: from now on we will consider the ndk installed in [ndk_path]

4. Create directory “tesseract” in [ndk_path]/sources
5. Copy Tesseract 2.03 in [ndk_path]/sources/tesseract. Copy them directly, for example “Makefile” should be seen directly in [ndk_path]/sources/tesseract.

6. Unzip all downloaded files (step 3), in the directory [ndk_path]/sources/tesseract
7. In [ndk_path]/apps make a symbolic link to “mezzofanti_java_code”
After this step, the directory /sources/tesseract should look like:

-rw-r–r– 1 User None 170 Jul 8 14:38 AUTHORS
-rwxr-xr-x 1 User None 12916 Jul 8 14:38 Android-multiple-libs.mk
-rwxr-xr-x 1 User None 5997 Jul 9 17:40 Android.mk
-rwxr-xr-x 1 User None 71 Jul 9 15:35 Application.mk
-rw-r–r– 1 User None 17751 Jul 8 14:38 BUILD
-rw-r–r– 1 User None 1058 Jul 8 14:38 COPYING
-rw-r–r– 1 User None 3014 Jul 8 14:38 ChangeLog
-rw-r–r– 1 User None 9236 Jul 8 14:38 INSTALL
-rw-r–r– 1 User None 1223 Jul 8 14:38 LICENSE
-rw-r–r– 1 User None 933 Jul 8 14:38 Makefile.am
-rw-r–r– 1 User None 45 Jul 8 14:38 NEWS
-rw-r–r– 1 User None 33 Jul 8 14:38 OWNERS
-rw-r–r– 1 User None 5014 Jul 8 14:38 README
-rw-r–r– 1 User None 8913 Jul 8 14:38 ReleaseNotes
-rw-r–r– 1 User None 286 Jul 8 14:38 StdAfx.cpp
-rw-r–r– 1 User None 778 Jul 8 14:38 StdAfx.h
-rw-r–r– 1 User None 7309 Jul 8 14:38 acinclude.m4
-rw-r–r– 1 User None 33298 Jul 8 14:38 aclocal.m4
drwxr-xr-x+ 2 User None 0 Jul 8 14:38 aspirin
drwxr-xr-x+ 2 User None 0 Aug 31 18:35 ccmain
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 ccstruct
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 ccutil
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 classify
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 config
-rwxr-xr-x 1 User None 10209 Jul 8 14:38 configure.ac
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 cutil
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 dict
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 dlltest
drwxr-xr-x+ 2 User None 0 Jul 8 14:38 doc
drwxr-xr-x+ 2 User None 0 Jul 8 14:38 helium
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 image
drwx——+ 3 User None 0 Aug 28 10:28 java
drwx——+ 9 User None 0 Sep 1 17:22 mezzofanti_java_code
-rwx——+ 1 User None 42136 May 29 21:54 liblog.so
-rwxr-xr-x 1 User None 437 Jul 8 14:38 makemoredists
drwxr-xr-x+ 3 User None 0 Jul 8 14:38 neural_networks
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 pageseg
-rwxr-xr-x 1 User None 2140 Jul 8 14:38 runautoconf
drwxr-xr-x+ 6 User None 0 Aug 31 18:39 tessdata
-rw-r–r– 1 User None 9200 Jul 8 14:38 tessdll.cpp
-rw-r–r– 1 User None 38429 Jul 8 14:38 tessdll.dsp
-rw-r–r– 1 User None 5421 Jul 8 14:38 tessdll.h
-rwxr-xr-x 1 User None 159480 Jul 8 14:38 tessdll.vcproj
-rwxr-xr-x 1 User None 38795 Jul 8 14:38 tesseract.dsp
-rwxr-xr-x 1 User None 2117 Jul 8 14:38 tesseract.dsw
-rwxr-xr-x 1 User None 4709 Jul 8 14:38 tesseract.sln
-rw-r–r– 1 User None 5905 Jul 8 14:38 tesseract.spec
-rwxr-xr-x 1 User None 125592 Jul 8 14:38 tesseract.vcproj
-rw-r–r– 1 User None 560 Sep 1 17:36 test
drwxr-xr-x+ 3 User None 0 Aug 28 10:28 testing
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 textord
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 training
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 viewer
drwxr-xr-x+ 2 User None 0 Aug 28 10:28 wordrec

8. Patch the Tesseract files, with this Tesseract_ccmain_patch.zip (found on the google code). Also do copy over the Android.mk file, over the original one in order to be able to compile the code.

9. In Eclipse, import the Mezzofanti project.

enjoy ;)

Sliding drawer

July 16th, 2009 No comments

(Android phone – how-to/example)

You can integrate the cool sliding-drawer that you use all the time in Home application on Android (the small arrow on the right/bottom of the screen, that displays all the available applications).

One sliding-drawer has two components:
- the handle: most of the time a picture (the picture that you drag). Be careful not to use a big image, or text.
- the content: text, images etc

< SlidingDrawer
android:id="@+id/drawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:handle="@+id/handle"
android:content="@+id/content"
android:orientation="horizontal">

< LinearLayout
android:id="@id/handle"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="fill_parent">

< ImageView android:id="@+id/IconSlide"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/left_48">
< /ImageView>
< /LinearLayout>

< LinearLayout
android:id="@id/content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="fill_parent">

< !-- INSERT in here the content of the sliding-drawer -->

< /LinearLayout>

< /SlidingDrawer>

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:

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: