18

Easy one.

I've gone through a few guides and tutorials, and they're quite clear on how to start an activity (with intent).

However, how do I create a new activity in Eclipse? I can probably do this by hand, but then I have to modify the R file, which is auto-generated, and add a new xml layout.

Lukas
  • 1,346
  • 7
  • 24
  • 49
Hein du Plessis
  • 3,305
  • 6
  • 34
  • 51

7 Answers7

56

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.

LocalPCGuy
  • 6,006
  • 2
  • 32
  • 28
ShadowGod
  • 7,891
  • 3
  • 28
  • 31
  • Perfect thanks, the right-click / new class is what I was missing. It also seems eclipse allows you to create a new class automatically if you reference it without it being defined first. I also used the manifest GUI screen to modify the xml, nice and quick. – Hein du Plessis Sep 13 '10 at 07:10
  • @ShadowGod: your answer is helpful but this is all about registering new Activity. Can you tell me how could I create the `layout xml` for this new activity (just like main.xml). – Mohammad Faisal Oct 21 '12 at 06:25
  • 2
    @ShadowGod This is a nice thorough answer, but if you use Eclipse's "New" wizard, it'll create the new Activity's class and layout for you, and even add the required entries to the project's AndroidManifest.xml file. It's definitely the best and easiest way to accomplish this. – Argus9 May 31 '13 at 15:19
23

I know this is an old question, but I know there are still people with this same question(I did up until today)

If you add a new activity to your manifest file, there's a special link to click on to automatically create the new Activity, complete with the onCreate() method ready to be filled in.

Open the AndroidManifest.xml, and go to the 'Application' tab. Under 'Application Nodes', find and click the 'Add' button. You will likely create a new element at the top level, so select that option, highlight 'Activity', and press OK.

Once you've created the Activity, go to the 'Attributes for Activity' and fill in the name. Once you've filled in the name you want, click on the blue 'Name*' link next to the field. The new file wizard will show up, and all you have to do is press OK.

Voila! New Activity, registered in the manifest and as a ready-to-go Java class.

Indigenuity
  • 9,332
  • 6
  • 39
  • 68
5

You create the activity by extending the activity class . Once you have creatd the activity class , You need to add the activity in the androidmanifest file specifying the properties for the activity...

A sample one would be something like this ...

<activity android:name=".JsonActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

The action here indicates that it is the one that starts first ..

I dont think you need to modify the R.java file ... Once you add these in the android manifest file and save it automatically gets updated. Also the things that u added like the layouts, menus, strings, id's etcc.... in the various xml files also get automatically updated...

Correct me if i am wrong ...

Flash
  • 2,901
  • 5
  • 38
  • 55
4

I tried searching for this question on Google and haven't seen this solution yet, so I thought I'd post it here.

In Eclipse, you can click on the "New" button on the toolbar. Under Android, select Android Activity, and run through the wizard. This is the best solution by far, since it lets you set up a layout and an Activity all in one, while also updating the Manifest for you.

Argus9
  • 1,863
  • 4
  • 26
  • 40
1

How to add New Activity Eclipse step by Step:

  1. Stpe1:Double click on the androidManifest
  2. Step2:on the Menu bar click Aplication
  3. Step3:Scroll down to application node and CLick add button
  4. Step 4:click select Activity and Ok
  5. step 5:clik on the the Texte(Name* Note:make sur u clik on the texte not into the textbox )
  6. step6:there a new Java Class dialog
    ## Heading ##write the classe name ## Heading ## check checkbox construct from the super classe and and ok..
0

There is also the tried and tested method of starting with one of the samples and going from there.

The Hello tutorial is as good a starting point is any, just select the create from existing sample option.

The latest update to the eclipse plugin even includes a tool to rename your package should you change your mind though I haven't used it yet so can't say if it works. (Right click on the package then select Android Tools, Rename Application Package).

FixerMark
  • 739
  • 6
  • 15
  • I've done the hello world, also the beyond hello world. In none of the guides do they mention what the "proper" was is to create a new activity. – Hein du Plessis Sep 11 '10 at 19:45
  • Sorry I should have made it clear that I meant that you could use helloworld (or any other sample if it has the functionality you need) as a template. Doing it that way basic resources such as layout etc are already created and linked for you. Just saves a bit of typing. (...and reduces the chances of getting it wrong!) – FixerMark Sep 11 '10 at 20:03
  • Ok understood, but I prefer to do it manually this time, just to understand. – Hein du Plessis Sep 13 '10 at 07:12
0

It is important to say that if you type the desired name for the new Activity on Name box, a dot must be put before the new name. Otherwise, the window to complete the creation of Java code will not open when you click on names link.