Best way to add Activity to an Android project in Eclipse?
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.
Android - Creating a new activity in Eclipse
Ok. Being a newbie myself I think the above two answers are thinking too much. He's asking very simply how to create a new activity in Eclipse.. I think this is what he wants:
A new Activity
in Eclipse is actually a Class
.
You would doubleclick 'src' on the left side in the Package Explorer, then highlight your 'com.' name, right click, select 'New' and then select 'Class'. Enter the Name
as NewActivity
and set the Superclass
to android.app.Activity
, then hit Finish.
When the NewActivity.java file opens up it should look like this:
package com.example.yourappname;
import android.app.Activity;
public class NewActivity extends Activity {
}
You can leave the Superclass
blank and add extends Activity
to the code itself if you prefer.
The final step is adding the Activity to your Manifest. So doubleclick AndroidManifest.xml to open it up and then click the 'Application' tab on the bottom. Next to the 'Application Nodes' box, click 'Add'. Highlight 'Activity' (the square box with a capital A) and click 'Ok'. Now look for the 'Attributes for Activity' box and enter a Name for the Activity and precede it by a period. In this example you'd enter '.NewActivity'.
And then you can add your onCreate()
code so it looks like this:
public class NewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_view);
//rest of the code
}
}
main_view
would be your main view xml file, main_view.xml
, that you would create in your layout directory.
To call the new Activity, your Intent
in the code (in a different Activity) to start a new Activity
looks something like this:
Intent startNewActivityOpen = new Intent(PresentActivity.this, NewActivity.class);
startActivityForResult(startNewActivityOpen, 0);
And that's it, you have the code to call the new activity and you created it. I hope this helps someone.
How to add new activity to existing project in Android Studio?
To add an Activity
using Android Studio.
This step is same as adding Fragment, Service, Widget, and etc. Screenshot provided.
[UPDATE] Android Studio 3.5. Note that I have removed the steps for the older version. I assume almost all is using version 3.x.
- Right click either java package/java folder/module, I recommend to select a java package then right click it so that the destination of the Activity will be saved there
- Select/Click New
- Select Activity
- Choose an Activity that you want to create, probably the basic one.
To add a Service
, or a BroadcastReceiver
, just do the same step.
Add activity node android eclipse
Make sure you extend Activity and import android.app.Activity like this:
import android.app.Activity;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Then try going to Project -> Clean menus on eclipse.
If that doesnt help try right clicking your project and click refresh.
And if it still doesnt show the activity on AndroidManifest, manually add the following line on the XML after making appropriate changes
<activity android:name="MyActivity"></activity>
Hope that helps. Good luck.
android eclipse not creating blank activity
its currently bugged if you updated to 23.0. see https://code.google.com/p/android/issues/detail?id=72419
Related Topics
Show an Alert Dialog in Broadcast Receiver After a System Reboot
What Proguard Configuration Do I Need for Firebase on Android
Android: How to Periodically Send Location to a Server
How to Use a Custom Typeface in a Widget
Multi Flavor App Based on Multi Flavor Library in Android Gradle
Android How to Create My Own Activity and Extend It
Set Inputtype for an Edittext Programmatically
Findviewbyid VS View Holder Pattern in Listview Adapter
Actionbaractivity Is Deprecated
Permanently Hide Navigation Bar in an Activity
Getrunningtasks Doesn't Work in Android L
Is a Relativelayout More Expensive Than a Linearlayout
How to Hide an Item from Recycler View on a Particular Condition
Adding Table Rows Dynamically in Android
How to Get Access to Raw Resources That I Put in Res Folder
Datepicker Dialog Without Calendar Visualization in Lollipop [Spinner Mode]