About Box for an Android App

How to Build a Reusable About Dialog for Your Android Apps

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, http://en.wikipedia.org/wiki/About_box, 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. This tutorial presents the code for an About Box class that can be dropped straight into any App.

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.

[code lang=”Java”]static String VersionName(Context context) {
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(),0).versionName;
} catch (NameNotFoundException e) {
return "Unknown";
}
}[/code]

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 its setTitle(), setMessage() and show() methods an About Box can be put together quickly; but using the Android Linkify class and a custom layout the About Box can be improved. In the About text any URLs (such as App help pages on the web) and email addresses (useful for a support email link) can be made clickable. Try all the code below in a new project.

In Eclipse use the File menu, select New then Android Project. Fill in the Project Name, here My App is used. Click Next and select the build target from the installed APIs, any of the installed APIs will work, it can be changed later if required. Click Next and enter the Package Name in the required format, e.g. biz.tekeye.abouttest, leave Create Activity checked and enter Main as the Activity name, click Finish. Create a new custom layout, with the res/layout folder selected in the Package Explorer select New then Android XML File. In the File box enter aboutbox.xml, select ScrollView for the Root Element and click Finish.

ADT New Layout Dialog

With aboutbox.xml open select the XML view in the editior (using the tabs at the bottom of the edit area). Replace the contents with the following code (see Copying Code from the Articles for tips):

[code lang=”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>[/code]

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).

Highlight the package in the src folder and use the File menu to select New then Class. In the Name field enter AboutBox.

ADT New Java Class Dialog

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. Open the AboutBox.java file and replace the AboutBox class with this class code (imports for Context, NameNotFoundException, Activity, SpannableString, View, TextView, LayoutInflater, ViewGroup, InflateException, Linkify and AlertDialog will be required):

[code lang=”Java”]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.ic_launcher)
.setPositiveButton("OK", null)
.setView(about)
.show();    //Builder method returns allow for method chaining
}
}[/code]

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, like this:

[code lang=”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>[/code]

Showing the About Box requires only one line of code, shown here on a button click (imports for OnClickListener and View are required):

[code lang=”java”]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);
}
});
}
}[/code]

The Button is added to the main.xml file.

[code lang=”xml”]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About" />
</LinearLayout>[/code]

Android About BoxTo 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.

Download some example code that covers this article ready for importing into an Android project. The code can also be accessed via the Android Example Projects page. See the article Move Android Code Between PCs Running Eclipse on how to import example code into an Eclipse project. A version of this article was produced for the Android Cookbook.

2 thoughts on “About Box for an Android App

    • Is the AboutBox class in the same package as the Activity? Both source files should have the same package name at the top of the file. If not you will need to import the About box package, or prefix it with the full package name. Also check that there are no errors in the AboutBox class. If the class has errors it will not compile and will not be resolved in the main class. Download working code from our Android Example Projects page.

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.