tags:

views:

87

answers:

3

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 by then I have to modify the R file which is auto-generated. I can create a new xml layout.

Thanks for helping out a newb.

+1  A: 

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 ...

ravi
Thanks, going to try it. How do I link the layout xml file with the specific activity?
Hein du Plessis
just write setContentView(R.layout.XMLFILE_NAME). The XML FILE should be created inside the R.layout directory.
ravi
Thanks, it didnt show the first time because the first letter was a capital! Gotta love java ;)
Hein du Plessis
A: 

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
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
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
Ok understood, but I prefer to do it manually this time, just to understand.
Hein du Plessis
+3  A: 

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, so if your Intent in the code to start a new Activity looks something like this:

Intent startNewActivityOpen = new Intent(PresentActivity.this, NewActivity.class);
startActivityForResult(startNewActivityOpen, 0);

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' to match the code above and hit Finish. When the NewActivity.java file opens up just add 'extends Activity' so it looks like this:

    public class NewActivity extends Activity {

}

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);


        }
}

main_view would be your main view xml file, main_view.xml, that you would create in your layout directory.

And that's it, you have the code to call the new activity and you created it. I hope this helps someone.

ShadowGod
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