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.

Tutorial #8 - Setting Up An Activity

Of course, it would be nice if those “Help” and “About” menu choices that we added in the previous tutorial actually did something.

In this tutorial, we will define another activity class, one that will be responsible for displaying simple content like our help text and “about” details. And, we will arrange to start up that activity when those action bar items are selected. The activity will not actually display anything meaningful yet, as that will be the subject of the next few tutorials.

This is a continuation of the work we did in the previous tutorial.

You can find the results of the previous tutorial and the results of this tutorial in the book’s GitHub repository.

Step #1: Creating the Stub Activity Class and Manifest Entry

First, we need to define the Java class for our new activity, SimpleContentActivity.

Right-click on your main/ source set directory in the project explorer, and choose New > Activity > Empty Activity from the context menu. This will bring up a new-activity wizard:

Android Studio New Activity Wizard
Figure 240: Android Studio New Activity Wizard

Fill in SimpleContentActivity in the “Activity Name” field and uncheck the “Generate Layout File” checkbox. Leave “Launcher Activity” unchecked, and uncheck the “Backwards Compatibility (AppCompat)” checkbox. If the package name drop-down is showing the app’s package name, leave it alone. On the other hand, if the package name drop-down is empty, click on it and choose the app’s package name. Leave the source language drop-down set to Java. Then click on Finish.

At this point, your SimpleContentActivity class should look like:

package com.commonsware.empublite;

import android.os.Bundle;

public class SimpleContentActivity extends Activity {

  protected void onCreate(Bundle savedInstanceState) {

Step #2: Launching Our Activity

Now that we have declared that the activity exists and can be used, we can start using it.

Go into EmPubLiteActivity and modify onOptionsItemSelected() to add in some logic in the and branches, as shown below:

  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        Intent i=new Intent(this, SimpleContentActivity.class);


        i=new Intent(this, SimpleContentActivity.class);



In those two branches, we create an Intent, pointing at our new SimpleContentActivity. Then, we call startActivity() on that Intent. Right now, both help and about do the same thing — we will add some smarts to have them load up different content later in this book.

You will need to add an import for android.content.Intent to get this to compile.

If you run this app in a device or emulator, and you choose either the Help or About menu choices… nothing much appears to happen. In reality, what happens is that our SimpleContentActivity appeared, but empty, as we have not given it a full UI yet.

In Our Next Episode…

The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.