When adding an activity to an existing Android project, I manually create a new class - is that the best / preferred way? How do others handle that?
8 Answers
You can use the "New Class" dialog, but that leaves other steps you need to do by hand (e.g. adding an entry to the manifest file). If you want those steps to be automated, you can create the activity via the manifest editor like this:
- Double click on AndroidManifest.xml in the package explorer.
- Click on the "Application" tab of the manifest editor
- Click on "Add.." under the "Application Nodes" heading (bottom left of the screen)
- Choose Activity from the list in the dialog that pops up (if you have the option, you want to create a new top-level element)
- Click on the "Name*" link under the "Attributes for" header (bottom right of the window) to create a class for the new activity.
When you click Finish from the new class dialog, it'll take you to your new activity class so you can start coding.
Five steps might seem a lot, but I'm just trying to be extra detailed here so that it's clear. It's pretty quick when you actually do it.

- 28,701
- 14
- 75
- 97
-
23One caveat is that you must remember to prepend the "." to your Activitiy name, or you will end up with all sorts of nonsensical err msgs. – B. Clay Shannon-B. Crow Raven Dec 03 '11 at 21:59
-
I tried doing this with Eclipse 3.5.2 on Ubuntu 10.04. When the add dialog comes up, it has two selections... No, now only one "Create a new element at the top level, in Application." Ne;low that there is a text entry field and below that, a box with selections for Activity, Activity Alias, Meta Data and so on. Below that there are buttons for "Cancel" and "OK". Upon the first character entered in the text field, all selections disappear and the "OK" button becomes gray. Bug or am I doing it wrong? Thx, hank – HankB Dec 29 '11 at 17:35
-
I was doing it wrong. The dialog box creates an empty activity and once created, I need to use the form next to it (to the right in my display) to provide the additional attributes. – HankB Dec 29 '11 at 17:45
-
Awesome.... This is definately the easy way to do it. Of Course you dont have to do this first. I create the class, then the xml... after that, follow these instructions. select the top level Element>Activity then hit ok. go to the attributes for the Activity to the right of the app. nodes and click browse next to name... sometime you have to wait a minute for your class to show up, but then it does and you click it... done. – Dave_P Jan 23 '12 at 20:43
-
How to invoke this new window in app? – Yoda Jul 11 '12 at 14:28
-
4Like this: Intent intent = new Intent(OldActivity.this, NewActivity.class); startActivity(intent); No need to shout ;) – user460847 Jul 16 '12 at 18:31
-
I was not presented to any kind of "Finish" button. Where is that? – Eduardo Jul 22 '12 at 12:48
-
1@Eduardo, be sure to click the Name* link in the bottom right hand corner, that should bring up a class creation window with your "Finish" button. – Will Andrew Jul 22 '13 at 16:07
-
2it does not create the activity layout that is very important. better is this way http://stackoverflow.com/questions/6253578/how-to-add-second-activity-in-android – coto Sep 29 '13 at 07:46
It is now much easier to do this in Eclipse now. Just right click on the package that will contain your new activity. New -> Other -> (Under Android tab) Android Activity.
And that's all. Your new activity is automatically added to the manifest file as well.

- 2,662
- 4
- 28
- 46
-
1you have to add explicitly in manifest file about your activity information .It is not getting added automatically. – Akshay Aug 13 '12 at 13:18
-
9After New -> Other -> Android Activity -> Blank Activity and giving it a unique name, at next step you will see eclipse shows you a "changes to be performed" list. In this list you can see manifest file, strings.xml etc already checked. So, unless you uncheck them, it is added automatically – mamba4ever Aug 16 '12 at 08:55
-
2What if you want the activity to be created in a specific package ? – Mr_and_Mrs_D Mar 03 '13 at 15:13
An easy method suggested by Google Android Developer Community.

- 68,075
- 43
- 96
- 126
-
1FYI, this option did not exist when I started building Android apps in 2009 :-) – Eno Nov 10 '14 at 17:32
-
I'm only just now discovering it. The curse of being an early adopter -- you don't notice the new stuff. – Edward Falk Mar 29 '16 at 19:42
I just use the "New Class" dialog in Eclipse and set the base class as Activity. I'm not aware of any other way to do this. What other method would you expect to be available?

- 183,023
- 24
- 297
- 295
-
1Just curious if maybe there was a plugin that automated some of it or provided a step-by-step wizard. – Eno Feb 25 '10 at 22:50
The R.* classes are generated dynamically. I leave the "Build automatically" option on in the Project menu so that mine R.* classes are always up-to-date.
Additionally, when creating new Activities, I copy and rename old ones, especially if they are similar to the new Activity that I need because Eclipse renames everything for you.
Otherwise, as others have said, the File->New->Class command works well and will build your file for you including templates for required methods based on your class, its inheritance and interfaces.

- 3,050
- 24
- 30
For creating new Activity simply click ctrl+N one window is appear select android then another window is appear give name to that Secondary Activity.Now another Activity is created

- 21
- 1
There is no tool, that I know of, which is used specifically create activity classes. Just using the 'New Class' option under Eclipse and setting the base class to 'Activity'.
Thought here is a wizard like tool when creating/editing the xml layout that are used by an activity. To use this tool to create a xml layout use the option under 'New' of 'Android XML File'. This tool will allow you to create some of the basic layout of the view.

- 513
- 4
- 9
-
2Looks like IntelliJ IDEA has a wizard for creating activities - see http://www.jetbrains.com/idea/features/google_android.html – Eno Feb 13 '12 at 23:25
I have create a eclipse plugin to create activity in one click .
Just download the Plugin from https://docs.google.com/file/d/0B63U_IjxUP_GMkdYZzc1Y3lEM1U/edit?usp=sharing
Paste the plugin in the dropins folder in Eclipse and restart eclipse
For more details please see my blog
http://shareatramachandran.blogspot.in/2013/06/android-activity-plugin-for-eclispe.html
Need your comment on this if it was helpful...

- 61
- 5