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?
Solution 1:
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.
Solution 2:
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.
Solution 3:
An easy method suggested by Google Android Developer Community.
Solution 4:
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?
Solution 5:
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.