Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Creating a Reusable About Box Class

Author: Daniel Fowler -- Published? true -- FormatLanguage: W

Problem:

About Boxes are common in any application it is useful not to have to recode them for each new App.

Solution:

Write an About Box class that can be dropped straight into any new App.

Discussion:

The venerable About Box, whatever the Operating System, whatever the program the chances are it has an About option. There is a Wikipedia entry for it, [1], and it is useful for support:

Hello, there is a problem with my application?

Hi, can you press About and tell me the version number?

Since it is likely to be required again and again it is worth having a ready made About Box class that can be easily added to any new App that is developed. As a minimum the About should display a dialog with a title, e.g. About My App, the Version Name from the manifest, some descriptive text (loaded from a string resource) and an OK button.

The Version Name can be read from the PackageInfo class. (PackageInfo is obtained from PackageManager which itself is available from the App's Context). Here is a method to read an App's Version Name string.

static String VersionName(Context context) {
    try {
        return context.getPackageManager().getPackageInfo(context.getPackageName(),0).versionName;
    }
    catch (NameNotFoundException e) {
        return "Unknown";
    }
}

PageInfo can throw a NameNotFoundException (for when the class is used to find information on other packages), the exception is unlikely to occur, here it is just consumed by returning an error string. (To return the Version Code, the App's internal version number, swap versionName for versionCode and return an integer.)

With an AlertDialog.Builder and the setTitle(), setMessage() and show() methods soon has an About Box up; but using the Android Linkify class and a custom layout the About Box can be improved. In the About text any web addresses (such as App help pages on the web) and email addresses (useful for a support email link) can be made clickable. Save this layout into the res/layout folder as aboutbox.xml.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/aboutView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/aboutLayout"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
        <TextView android:id="@+id/aboutText"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textColor="#FFF"/>  
    </LinearLayout>
</ScrollView>

A ScrollView is required for when the About text is long and the screens are small (QVGA). Another advantage of the custom layout for the About Box text is that the look of the text can be modified (here set to white with a little padding).

The About Box class uses a Spannable to hold the text which can then be passed to Linkify via the TextView in the custom layout. The layout is inflated, the About text set and then AlertBuilder.Builder is used to create the dialog, the full code for the class is:

public class AboutBox {
    static String VersionName(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(),0).versionName;
        }
        catch (NameNotFoundException e) {
            return "Unknown";
        }
    }
    public static void Show(Activity callingActivity) {
        //Use a Spannable to allow for links highlighting
        SpannableString aboutText = new SpannableString("Version " + VersionName(callingActivity)+ "\n\n"
            + callingActivity.getString(R.string.about));
        //Generate views to pass to AlertDialog.Builder and to set the text
        View about;
        TextView tvAbout;
        try {
            //Inflate the custom view
            LayoutInflater inflater = callingActivity.getLayoutInflater();
            about = inflater.inflate(R.layout.aboutbox, (ViewGroup) callingActivity.findViewById(R.id.aboutView));
            tvAbout = (TextView) about.findViewById(R.id.aboutText);
        }
        catch(InflateException e) {
            //Inflater can throw exception, unlikely but default to TextView if it occurs
            about = tvAbout = new TextView(callingActivity);
        }
        //Set the about text 
        tvAbout.setText(aboutText);
        // Now Linkify the text
        Linkify.addLinks(tvAbout, Linkify.ALL);		
        //Build and show the dialog		
        new AlertDialog.Builder(callingActivity)
            .setTitle("About " + callingActivity.getString(R.string.app_name))
            .setCancelable(true)
            .setIcon(R.drawable.icon)
            .setPositiveButton("OK", null)
            .setView(about)
            .show();	//Builder method returns allow for method chaining  
    }	
}

Notice that the App's icon can be shown in the About Box title using setIcon(R.drawable.icon). String resources for the App's name and About Text are required in the usual res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My App</string>
    <string name="about">This is our App, please see http://www.example.com. Email support at support@example.com.</string>
</resources>

Showing the About Box requires only one line of code, shown here on a button click:

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.button1).setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) {
            	AboutBox.Show(Main.this);
    	    }
        });
    }
}

To reuse this About Box drop the aboutbox.xml into a project's res/layout folder, add a new class called AboutBox, replace the class code with the AboutBox class code above. Then just call AboutBox.Show() from a button or menu click. Web address and email addresses highlighted in the text can be clicked and invoke the browser or email client, very useful.

See Also:

[1]

[2]

Download:

The source code for this project can be downloaded from http://tekeye.biz/download/aboutbox.zip.
No records found.