{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
A few basic imports in an Android application program
Summary
This article looks at a few basic imports that an Android program might use.
 
Table of Contents

Listing 1. Some common import statements in an Android application

Figure 1. Activity, Window and TextView

import android.app.Activity

Listing 2. Commonly overridden methods from the Activity class

import android.os.Bundle

Listing 3. Saving a series of state variables into a Bundle object

Listing 4. Getting the values stored in a bundle

import android.view.Window

import android.widget.TextView

 

In an Android application file that contains a class that extends the Activity class, you might see the following import statements.

Listing 1. Some common import statements in an Android application

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;

Once you have imported a class from a different package, you will be able to refer to that class without specifying the fully qualified path for a specific class. So, after you have added the statement ‘import android.app.Activity;’ at the top of the file, you will be able to derive your class from Activity like ‘public class Snake extends Activity’ without having to say ‘public class Snake extends android.app.Activity’.

Now let’s look at each of these classes coming from the import statements.

Figure 1. Activity, Window and TextView

Activity, Window and TextView in Android

import android.app.Activity

The Activity class is perhaps the most important class for an Android application. Activity is any one thing that a user can do – and hence the main class in the application will be subclassed from this. For example, in the Snake game:

public class Snake extends Activity {

/* The Activity functionality is available here */

}

The Activity class provides several callbacks; you will override a few of them to add your application specific behavior.

Listing 2. Commonly overridden methods from the Activity class

@Override
public void onCreate(Bundle icicle) {
}

@Override
protected void onPause() {
}

@Override
public void onFreeze(Bundle outState) {
}

As the names suggest, onCreate() gets called when the activity is created. So, here you will setup the user interface and initialize any state-related variables, do the startup tasks, etc.

Both onPause() and onFreeze() are called when your view goes out of visibility. The state you save during onFreeze() will be passed to onCreate(), should this activity be killed (because of low-memory situation). As you can see from the definitions, both onCreate() and onFreeze() are passed a parameter of Bundle type.

import android.os.Bundle

Bundle is a utility class that lets you store a set of name-value pairs. You will always find this import along with the import for Activity class because both onCreate() and onFreeze() methods take Bundle as a parameter. Into a Bundle object, you can put integers, longs, strings, arrays, etc along with the keys to identify them. When needed, these values can be obtained by using those keys.

So, as you can see, a bundle object is ideal for storing a set of state variables in the onFreeze method; and the same state variables can be read back in the onCreate method.

So, for example, in the snake game a series of state variables (direction, move delay, score, snake trail, etc) can be stored in a Bundle object (whether they are arrays, integers, strings, etc.).

Listing 3. Saving a series of state variables into a Bundle object

public Bundle saveState() {
        Bundle map = new Bundle();

        map.putIntArray("mAppleList", coordArrayListToArray(mAppleList));
        map.putInteger("mDirection", Integer.valueOf(mDirection));
        map.putInteger("mNextDirection", Integer.valueOf(mNextDirection));
        map.putLong("mMoveDelay", Long.valueOf(mMoveDelay));
        map.putLong("mScore", Long.valueOf(mScore));
        map.putIntArray("mSnakeTrail", coordArrayListToArray(mSnakeTrail));

        return map;
 }

As you can see, the Bundle class has methods like putInteger, putLong, putIntArray, and so on.

The listing below shows how to get the values with the help of keys. As you can see, the counterpart to putInteger is getInteger; similarly getLong is to putLong; and so on.

Listing 4. Getting the values stored in a bundle

public void restoreState(Bundle icicle) {
        setMode(PAUSE);

        mAppleList = coordArrayToArrayList(icicle.getIntArray("mAppleList"));
        mDirection = icicle.getInteger("mDirection");
        mNextDirection = icicle.getInteger("mNextDirection");
        mMoveDelay = icicle.getLong("mMoveDelay");
        mScore = icicle.getLong("mScore");
        mSnakeTrail = coordArrayToArrayList(icicle.getIntArray("mSnakeTrail"));
}

import android.view.Window

While you can expect to see Activity and Bundle in every Android program, the Window and TextView classes are also pretty common.

You can obtain the Window for the current view via getWindow() method from the Activity class. The activity class also provides a shortcut method to setting the features of the window of the current activity, like:

       requestWindowFeature(Window.FEATURE_NO_TITLE);

The Window class contains constants for setting up the look of the window (for example, FEATURE_NO_TITLE takes out the title of the window, FEATURE_LEFT_ICON puts an icon on the left side of the title bar, and so on).

The Window class also contains a whole bunch of getters and setters for various window attributes like setTitle, setStyle, getStyle, getContext, and so on.

import android.widget.TextView

If your Android application has any user interface at all, it more than likely have a TextView. At its basic representation, TextView is a label (meaning, it just displays text that is not editable). As a label (uneditable TextView), it can be used as captions and textual displays.

You can declaratively define a TextView in an XML file. As shown below, the text alignment, size, color, etc. can be defined as attributes to a TextView element:

<TextView
 id="@+id/text"
 android:text="@string/snake_layout_text_text"
 android:visibility="visible"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true"
 android:textAlign="center"
 android:textColor="#ff8888ff"
 android:textSize="24sp"/>

You can get that particular TextView (as identified by id) with the help of the resource class (R) and the findViewById() method. The findViewById() method returns a View object, which is the base class for TextView. If you know that view is a TextView, you can simply change the type to TextView, as shown below:

        mSnakeView.setTextView((TextView) findViewById(R.id.text));

Take a Quick Quiz on this Article

1. Which of the following is NOT an overridable callback from the Activity class in an Android application?



: Activities are not bound to the database, yet
Question 1 of 5
Bookmark and Share This

More Articles With Similar Tags
icon-android-debug-toolbar.jpg
This article gives some quick pointers on how to start debugging an Android application, along with where to put the breakpoints and how to change the emulator sizes from within Eclipse.
icon-android-safari-browser-user-agent-string.jpg
This article talks about the basics of the browser (including the user agent string) used in the Android platform.
This article talks about various aspects of creating an SQLite database. It also uses Android SQLite shell as an example.
This article explains how to get pretty / nice output from the SQLite command line shell program.
icon-android-emulator-hvga-p-lunar-lander.jpg
See the images of various skins and screens of Android emulator with games (Lunar Lander and Snake) in them. Also shows how to change the skins of Android emulator while debugging your program.
About  Contact  Privacy Policy  Site Map