Android Dialog Theme and Window Decorations

While working on my first Android project, I ran into an interesting problem; how to add an icon to the window title of my application’s about dialog. My search for a solution only led to others asking this same question without any answers. Here is my solution to the problem in the hope that it may save you some time engineering your own.

Creating the activity for the about dialog was quite simple. The activity displays copyright notices, license information and the version of the application package.

Base Activity Components

Application manifest file (AndroidManifest.xml):


<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zunisoft.critters"
    android:versionCode="1"
    android:versionName="1.0.0">
    <application
        android:icon="@drawable/icon"
     android:label="@string/app_name">
        <activity
            android:name=".Critters"
            android:label="@string/app_name">
            <intent-filter>
                <action
                    android:name="android.intent.action.MAIN">
                    <category
                        android:name="android.intent.category.LAUNCHER">
                     </category>
                 </action>
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.about.About"
            android:label="@string/about_title">
        </activity>
    </application>
    <uses-sdk
        android:minsdkversion="3">
    </uses-sdk>
</manifest>

Activity source (About.java):


package com.zunisoft.critters.activities.about;

import static com.zunisoft.critters.Constants.APP_TAG;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.zunisoft.critters.R;
import com.zunisoft.critters.utility.Android;

/**
 * Application about box activity. This class displays information about the
 * application to the user.
 * 
 * @author krdavis
 */
public class About extends Activity {
 // Activity tag used by logging APIs
 private static final String ACT_TAG = "About";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  Log.d(APP_TAG, ACT_TAG + " -> onCreate()");

  // Initialize the layout
  super.onCreate(savedInstanceState);

  setContentView(R.layout.about);

  // Set the application version
  TextView text = (TextView) this.findViewById(R.id.app_version);
  text.setText(Android.getPackageVersion(this));

 }
}

Layout source (about.xml):


<scrollview
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/about_background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="15dip">
    <linearlayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:isScrollContainer="true"
        android:scrollbars="vertical">
        <linearlayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <imageview
                android:src="@drawable/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:layout_marginRight="10dp" />
            <linearlayout
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:orientation="vertical">
                <linearlayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical">
                <textview
                    android:text="@string/app_name"
                    android:textStyle="bold"
                    android:textColor="@color/about_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
                <textview
                    android:id="@+id/app_version"
                    android:textColor="@color/about_text"
                    android:textSize="11.5sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
                <textview
                    android:text="@string/about_text"
                    android:textColor="@color/about_text"
                    android:textSize="12.5sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp" />
                <textview
                    android:text="@string/about_code_copyright"
                    android:textColor="@color/about_text"
                    android:textSize="11.0sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp" />
                <textview
                    android:text="@string/about_art_copyright"
                    android:textColor="@color/about_text"
                    android:textSize="11.0sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="3dp" />
                <textview
                    android:text="@string/about_license"
                    android:textColor="@color/about_text"
                    android:textSize="11.0sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp" />
                <textview
                    android:text="@string/about_url"
                    android:textColor="@color/about_text"
                    android:textSize="11.0sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"/>
            </linearlayout>
        </linearlayout>
    </linearlayout>
</scrollview>

The above code and layout produces the following result:

Applying the Android Dialog Theme

The previous example while all well and good, doesn’t give a result that looks like a typical about dialog. To get the effect of a pop-up window, we can use the Android dialog theme. All that is needed is a small change to the application’s manifest file (AndroidManifest.xml):


<activity
    android:name=".activities.about.About"
    android:label="@string/about_title"
    android:theme="@android:style/Theme.Dialog">
</activity>

The result is starting to look like a real about dialog:

Adding the Window Decorations

The final piece of the puzzle is adding an icon to the window’s title area. I wanted to add this little extra as it makes my application look a little more “native”. Google Android applications such Maps decorate the about dialog in this manner.

I made the following changes to the About activity’s onCreate() method (About.java):


// Initialize the layout
super.onCreate(savedInstanceState);

Window w = getWindow();
w.requestFeature(Window.FEATURE_LEFT_ICON);

setContentView(R.layout.about);

w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
 android.R.drawable.ic_dialog_info);

The final result:

Please follow and like us:

11 Replies to “Android Dialog Theme and Window Decorations”

Leave a Reply

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