Android Tutorial (5) – How to create a new Activity class with the Manifest editor or without it

Behind any window or form in the Android mobile application there is an Activity instance. In order to develop a mobile application with multiple windows you must create, for each display, a new class that extends Activity class.

In this post we will see how to create a new Activity programmatically or using the Manifest WYSIWYG graphical editor that comes with the Android ADT Plugin for Eclipse.

Other topics that are part of this Android tutorial are accessible through Android Tutorial – Overview and contents.

 

How to create a new Activity class with the Manifest WYSIWYG graphical editor from the ADT plugin

 

1. Open the Android project manifest file, AndroidManifest.xml, by double-clicking on it in the project

Android Manifest Graphical Editor

Android Manifest Graphical Editor

2. in the Manifest WYSIWYG graphical editor select the Application tab or the Application link. The last tab, named AndroidManifest.xml, will open the XML text editor for the manifest file.

Graphical Editor for Android Manifest Application

Graphical Editor for Android Manifest Application

3. In the Android Manifest Application window of the editor, go to the Application Nodes section from the bottom of the page and click Add.

Android Manifest Graphical Editor for a new Activity

Android Manifest Graphical Editor for a new Activity

4. In the new component dialog select the Activity category (1) and then click OK (2).

5. After the last step we have a new Application Node of type Activity. Select it (it should be selected by default) and let’s edit its attributes in the right panel, Attributes for Activity:

New Android Activity Attributes

New Android Activity Attributes

5.1. Click on the Name link in the Attributes for Activity panel.

New Android  Activity Class Dialog

New Android Activity Class Dialog

5.2. Edit the class name in the New Java Class dialog and click Finish. Other properties like Source folder, Package, Superclass are already edited. In this example we name the class NewClass.

5.3. The editor will generate an initial form for the new class.

package eu.itcsolutions.eu;

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

public class NewClass extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);

	    // TODO Auto-generated method stub
	}

}

5.4. You can edit other attributes, like Theme, Label, Icon …., using the Attributes panel.

 

How to create a new Activity class programmatically

If you don’t want to use the Manifest WYSIWYG graphical editor, you can define a new Activity class like any other Java class.

1. The class is created as a common Java class, using the File –> New –> Class option in Eclipse. Name it NewActivity and set android.app.Activity as its superclass:

New Activity dialog

New Activity dialog

2. Because the ADT plugin is not so helpful when creating a new Activity class in this manner, you must edit it from scratch:

package eu.itcsolutions.android.tutorial;

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

public class SecondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
}

If you have problems with the examples or you don’t understand our explanations, put a question in the comments and we will help you. Also, any suggestion or comment that will improve this material is appreciated.

Other topics that are part of this Android tutorial are accessible through Android Tutorial – Overview and contents.

Like it? Then share this post or check the external adds. Sharing is the best way to appreciate the author.