• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to create a new activity in Android?

April 28, 2013 //  by Krishna Srinivasan//  Leave a Comment

In order to develop a mobile application which has many windows we need to create an activity for each window which extends activity class. When you create a simple helloworld application the first activity will be automatically created. You might be thinking how to add a new activity to your already developed helloworld application.

In This tutorial we will be discussing about how to add a new activity to your helloWorld application.

An activity can be created in two ways

  • Through AndroidManifest.xml
  • It can be created programmatically

1_android-activity

Creating a new Activity using AndroidManifest.xml

When you open your manifest file you can find the below section at the bottom of the manifest window in Eclipse IDE.

2_manifest_adding_activity

Click on the highlighted “Application” to add a new Activity. It takes you to the below screen.

3_manifest_activity2

Click the highlighted “Add” button. That takes you to the below screen.

4_manifest_activity3

Select the highlighted  “Activity” and click “OK” button. After clicking OK a new “Activity” node appears in the previously opened manifest window as below.

5_manifest_act_4

As you can notice a new “Activity” node has got added and also a new pane got appeared to the right of the “Application Nodes” pane. The name of the new pane is “Attributes for Activity”. Click on the highlighted “Name” section as in the above screen. That takes you to below screen.

6_Activity_add_4

Give a name to your activity. I have named it as “Activity_Second” and click the “Finish” button.
Your second activity has been created now. The initial code of your second activity is as below.


package my.android.app;
import android.app.Activity;
import android.os.Bundle;
public class Activity_Second extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
}
}

Creating a new Activity programmatically

You can create an Activity class like any other Java class.

  •  Open File->new->class

You will get the below screen.

7_new_act

Lets name the Activity as “Activity Second” and give the package name as “my.android”. Click “Finish” button.
The new activity is now created and the initial code is as below.


package my.android;

public class Activity_Second {

}

You can notice the difference between the code that got created when we create a new activity using AndroidManifest.xml and when it got created Programmatically. The Eclipse ADT doesn’t help you much when you create an Activity in this manner. You have to Edit it from the source. Hope you have understood how to create a new Activity. We shall discuss how to navigate from one activity to another in my next post. If you have any questions on creating the activity, please write it on the comments section.

Category: Google AndroidTag: Android

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How to Debug an Android application with LogCat?
Next Post: Resources API in Spring – Part 1 »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact