The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


The Android User Interface

The project you created in an earlier tutorial was just the default files generated by the Android build tools — you did not write any Java code yourself. In this chapter, we will examine the basic Java code and resources that make up an Android activity.

The Activity

The Java source code that you maintain will be in a standard Java-style tree of directories based upon the Java package you chose when you created the project (e.g., com.commonsware.android results in com/commonsware/android/). Android Studio will have that source, by default, in app/src/main/java/ off of the top-level project root.

If, in the new-project wizard, you elected to create an activity, you will have, in the innermost directory, a Java source file representing an activity class.

A very simple activity looks like:

package com.commonsware.empublite;

import android.app.Activity;
import android.os.Bundle;

public class EmPubLiteActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
}

Dissecting the Activity

Let’s examine this Java code piece by piece:

package com.commonsware.empublite;

import android.app.Activity;
import android.os.Bundle;

By default, the package declaration is the same as the one you used when creating the project. And, like any other Java project, you need to import any classes you reference. Most of the Android-specific classes are in the android package.

Remember that not every Java SE class is available to Android programs! Visit the Android class reference to see what is and is not available.

public class EmPubLiteActivity extends Activity {

Activities are public classes, inheriting from the android.app.Activity base class (or, possibly, from some other class that itself inherits from Activity). You can have whatever data members you decide that you need, though the initial code has none.


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

The onCreate() method is invoked when the activity is started. We will discuss the Bundle parameter to onCreate() in a later chapter. For the moment, consider it an opaque handle that all activities receive upon creation.

The first thing you normally should do in onCreate() is chain upward to the superclass, so the stock Android activity initialization can be done. The only other statement in our stub project’s onCreate() is a call to setContentView(). This is where we tell Android what the user interface is supposed to be for our activity.

This raises the question: what does R.layout.main mean? Where did this R come from?

To explain that, we need to start thinking about layout resources and how resources are referenced from within Java code.

Using XML-Based Layouts

The preview of this section is en route to Mars.