Understanding WordPress for Android – Part 3

I managed to squeeze in time for another blog post about the app. So here it is.

As I mentioned in my previous blogpost (
http://anirudhspider.wordpress.com/2011/06/08/understanding-wordpress-for-android-part-2/
), I will be explaining the code for the app’s splash screen here.

What is a Splash screen?

Quoting Wikipedia, “A splash screen is an image that appears while a game or program is loading. It may also be used to describe an introduction page on a website.” (
http://en.wikipedia.org/wiki/Splash_screen
)

The existing app has a really cool splash screen which I do not wish to lay my hands upon and spoil.

The splash screen that you see when the App starts

splashScreen.java

Let’s start with splashScreen.java. If you have downloaded the source code, you’ll find it in your src/org/wordpress/android/folder, or under src->org.wordpress.android in your Eclipse Package Explorer (left pane, by default). If you haven’t downloaded the source code, you can view it here: 
http://android.trac.wordpress.org/browser/trunk/src/org/wordpress/android/splashScreen.java

I have also posted it below for your convenience.

package org.wordpress.android;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import android.widget.TextView;

public class splashScreen extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        getWindow().setFormat(PixelFormat.RGBA_8888);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);

        setContentView(R.layout.splashscreen);

        //Display the current version number
        PackageManager pm = getPackageManager();
        try {
			PackageInfo pi = pm.getPackageInfo("org.wordpress.android", 0);
			TextView versionNumber = (TextView) findViewById(R.id.versionNumber);
            versionNumber.setText("Version " + pi.versionName);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        new Handler().postDelayed(new Runnable(){
            public void run() {
                 /* Create an Intent that will start the Main WordPress Activity. */
                 Intent mainIntent = new Intent(splashScreen.this,wpAndroid.class);
                 splashScreen.this.startActivity(mainIntent);
                 splashScreen.this.finish();
            }
       }, 2900); //2900 for release

}
}

Note that the line numbers refer to the above source code.
Lines 3-12: They are the necessary import statements. In case you miss any import statements and Eclipse shows an error, press Ctrl+Shift+O (in Linux and Windows). Eclipse will automatically find and add the necessary import statements.

Line 14: public class splashScreen extends Activity {
This is where the code actually starts. splashScreen being an Activity obviously extends the Activity class. (In case you do not know what an Activity is, an Activity is what the user is actually able to view. For more details: 
http://developer.android.com/reference/android/app/Activity.html
 )

Lines 17-19: 
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

The onCreate function is an integral part of the Activity life cycle and as the name implies, it is called once the Activity is created. We are overriding the onCreate method. Whenever the onCreate method is overridden, we must call super.onCreate() to save the state of the previous activity. ‘icicle’ is the name of the Bundle objects that are passed to the onCreate method. This is the default name provided in the earlier versions of the SDK. The new SDK defaults the name to ‘savedInstanceState’.

Lines 20&21:
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);

Line 20 sets the format. Can you see that the background gradient is uniform and non-patchy? It is because of the FLAG_DITHER flag.

Line 23: setContentView(R.layout.splashscreen);
Sets the xml layout provided in splashscreen.xml. Lets have a quick look at it.

splashscreen.xml

If you have downloaded: found at res/layout/splashscreen.xml
If you haven’t: 
http://android.trac.wordpress.org/browser/trunk/res/layout/splashscreen.xml

Code:

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center|center"
android:background="@drawable/home_gradient"
android:orientation="vertical">
<ImageView android:layout_marginTop="-60dip" android:paddingLeft="20dip" android:paddingRight="20dip" android:scaleType="centerInside" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/wordpress_logo" android:src="@drawable/wordpress_home"></ImageView>
<TextView android:text="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:typeface="serif"
android:shadowDx="0"
android:shadowDy="2"
android:shadowRadius="1"
android:shadowColor="#FFFFFF"
android:textColor="#444444"
android:textSize="20dip"
android:id="@+id/versionNumber" android:gravity="bottom">
</TextView>
</LinearLayout>

As you can see, it contains a vertical LinearLayout with 2 elements: an ImageView and a TextView. The ImageView renders the logo while the TextView provides the version number of the app.

Back to splashscreen.java

Lines 25-34:

//Display the current version number
PackageManager pm = getPackageManager();
try {
PackageInfo pi = pm.getPackageInfo("org.wordpress.android", 0);
TextView versionNumber = (TextView) findViewById(R.id.versionNumber);
versionNumber.setText("Version " + pi.versionName);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Obtain the version number from the package (from the manifest xml?) and sets it to the TextView.

Lines 36-42:

new Handler().postDelayed(new Runnable(){
public void run() {
/* Create an Intent that will start the Main WordPress Activity. */
Intent mainIntent = new Intent(splashScreen.this,wpAndroid.class);
splashScreen.this.startActivity(mainIntent);
splashScreen.this.finish();
}

An Intent is an abstract description used to jump from one Activity to another. The Intent mainIntent is used to transfer control from the current splashScreen.java file to wpAndroid.java. (The number 2900 in Line 43 is the number of milliseconds for which the splash screen is to be displayed.)

What does that mean? Yeah, you got that right. Coming up next, wpAndroid.java!

About these ads

One Comment on “Understanding WordPress for Android – Part 3”

  1. Johannes says:

    Really like this whole “Understanding WordPress for Android” series! Good introduction into basic Android dev. machanisms


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.