Best way to add Activity to an Android project in Eclipse?


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?

7/2/2012 3:59:14 AM

Accepted Answer

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:

  1. Double click on AndroidManifest.xml in the package explorer.
  2. Click on the "Application" tab of the manifest editor
  3. Click on "Add.." under the "Application Nodes" heading (bottom left of the screen)
  4. 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)
  5. 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.

3/10/2011 9:34:32 PM

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow