Thursday, 22 July 2010

Online Help for Andoid

A full tutorial on how to add HTML bases on-line help to Android applications. ...

One of the things I noted about Android is that there is no online help system inside the framework. So how would one go about it? I browsed my “Beginning Android 2” Book for the best way to go about it. And I found the android.webkit.WebView class. Embed an HTML browser into my seem to be just right for the task.

But could you load an html file from the apk file? The book would not say. “Pro Android 2” would not say either. Good books really but sometimes not deep enough.

I searched the internet and the answer is yes. But that is just the beginning. Most “tutorials” are missing some important facts.

So here the full tutorial on how the add HTML based on-line help to and Android Application.

Menu

First you need an Menu entry with icon. You find an appropriate icon in the Android Sources. it has the name ic_menu_help.png.

Layout

Then you need a layout. Many tutorials suggest LinearLayout for simple stuff but I found out that FrameLayout is better suited for layout with just one element:

<FrameLayout
android:layout_height='match_parent'
android:layout_width='match_parent'
android:paddingBottom='8dp'
android:paddingLeft='8dp'
android:paddingRight='8dp'
android:paddingTop='8dp'
xmlns:android='http://schemas.android.com/apk/res/android'
>
<WebView
android:id='@+id/Help'
android:layout_height='fill_parent'
android:layout_width='fill_parent'
></WebView>
</FrameLayout>


Activity

Where there is a layout you also need an activity. A pretty simple one:


/**
* <p>
* A simple activity to display some on-line help
* </p>
*
* @author "Martin Krischik" <krischik@users.sourceforge.net>
*/
public class Help_Activity
extends
android.app.Activity
{
/**
* logging tag
*/
private static final String TAG = Help_Activity.class.getName ();

/**
* Called when the activity is first created.
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
public void onCreate (final android.os.Bundle savedInstanceState)
{
android.util.Log.d (Help_Activity.TAG, "+ onCreate");
android.util.Log.v (Help_Activity.TAG, " > savedInstanceState = "
+ savedInstanceState);

super.onCreate (savedInstanceState);
this.setContentView (net.sourceforge.uiq3.fx602p.R.layout.help);
final android.webkit.WebView View =
(android.webkit.WebView) this.findViewById (net.sourceforge.uiq3.fx602p.R.id.Help);
final String Help_File_Name = "file:///android_asset/help.html";
final android.net.Uri Help_File_URI = android.net.Uri.parse (Help_File_Name);

android.util.Log.v (Help_Activity.TAG, " > Help_File_URI = " + Help_File_URI);

View.loadUrl (Help_File_URI.toString ());

android.util.Log.d (TAG, "- onCreate");
return;
} // onCreate
} // Help_Activity


I guess Java-Only developers might cringe at my capitation and the fact that I did not use import. But at least for the 2nd you must see the advantage. Or have you never been upset about a tutorial where the import statements where missing and you had to search for the package name?

Do note that comment out all «d» and «v» logging before release. No problem with Vim.

Help file

This took me longest to figure out. Some tutorials put the HTML page into «…/raw/». Some spoke of «.../web/». It took met some time to figure out a working combination. In the end I put the file into «src/main/resources/help.html». Note that I have a mostly maven compatible directory layout and if you use eclipse then you need to add «src/main/resources» as source folder.

The important part is that I stored the file completely separate from the resource directory which the android tools pre-process and I did not use a sub-directory.

No comments:

Post a Comment