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.
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 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
How to start android activity from eclipse with custom Intent
If your still using eclipse you probably need need to create a simple ant script with a custom task to execute the test. ADB shell has a command to start activities where you can also specify extra's
am [start|instrument]
am start [-a <action>] [-d ]
[-t <mime_type>] [-c <category> [-c <category>] ...]
[-e <extra_key> <extra_value>
[-e <extra_key> <extra_value> ...]
[-n <component>] [-D] [<uri>]
am instrument [-e <arg_name> <arg_value>] [-p <prof_file>] [-w] <component>
You would pass them like this:
am start -a android.intent.action.VIEW -c android.intent.category.DEFAULT -e foo bar -e bert ernie -n org.package.name/.MyCustomActivity
P.S. don't forget the dot before the activity.
This can be translated to an ant target which you should put in the ant script.
<target name="run">
<exec executable="adb">
<arg value="shell"/>
<arg value="am"/>
<arg value="start"/>
<arg value="-a"/>
<arg value="android.intent.action.MAIN"/>
<arg value="-e"/>
<arg value="extra_key extra_value"/>
<arg value="-n"/>
<arg value="{package.name}/{activity}"/>
</exec>
</target>
which you can execute like this: ant debug install run
How to run ant files from eclipse see:
- Eclipse Help ant
- Eclipse Help Application launcher
Eclipse doesn't create Main Activity and layout
I had the same problem yesterday after updating the latest packages on the Android SDK Manager.
Fixed it by updating the Eclipse plugin: Run Eclipse
> Help
> Install new software
> Paste the URL
https://dl-ssl.google.com/android/eclipse/ then press Enter
. After updating the plugin, I created a test project with the main activity and menu was created by default as normal.
Related Topics
Android Studio Could Not Find Any Version That Matches Com.Android.Support:Appcompat-V7:+
Does Android Service Run from a Separated Thread Instead of UI
Service Intent Must Be Explicit: Intent
Using Onbackpressed() in Android Fragments
How to Change Option Menu Icon in The Action Bar
Android Studio Where Is Gradle.Properties File
Fragment in Viewpager Not Restored After Popbackstack
Get Application Installed Date on Android
How to Display a One Time Welcome Screen
Items Inside Gridview Getting Repeated When Screen Scrolls
Not Able to Debug App in Android Studio
How to Debug on a Real Device (Using Eclipse/Adt)
Flutter Insecure Http Is Not Allowed by Platform