Android - Creating a new activity in Eclipse [duplicate] - android

This question already has answers here:
Best way to add Activity to an Android project in Eclipse?
(8 answers)
Closed 9 years ago.
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.

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.

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.

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

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.

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

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

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.

Related

New activity without xml layout in Android studio

When I create new Activity class in Android studio , two XML layout files will generate automatically.
One of them have same name with my Activity and other XML file have name like : Content_[activity name].XML
Now, how can i turn off this auto generating work in Android Studio?
I want to create new activities in android studio without any XML layout
file.
Follow these steps
1) Right click on your package name in which you want to create Activity.
2) Move your mouse cursor to New->Activity->Empty Activity.
3) Click on Empty Activity. you will see following window. "Unmark Generate Layout File"
4) Click Finish
Done...!!
If you don't want Android to Studio to generate the xml files for you, You must do every steps on your own.
Create a java class which extends from activity.
Create a layout resource file.
Override OnCreate method in your activity and set your layout.
Add the new activity in manifest.
That's it.

how to make a button go from MainActivity page to another (Android)

I noticed that even though I made a new xml named drink, there is no ".java" file with it. Do I need to make one or do i add it on to MainActivity.java?
This code is in my MainActivity.java and the Intent line is saying that drink cannot be resolved to a type.
public void calculate(View view){
Log.i("clicks","You Clicked B1");
Intent i=new Intent(MainActivity.this, drink.class);
startActivity(i);
}
This is in my manifest:
<activity
android:name=".drink">
</activity>
This is a post before I knew android. I didn't realize you have to make the java file and xml file.
This code is in my MainActivity.java and the Intent line is saying
that drink cannot be resolved to a type.
This means that you don't have a "drink" class. you have to create a new class called "drink" that extends Activityin the application package.
Without any further code to go on (including knowing what your drink class and your manifest.xml looks like), I would check to make sure that the package name specified in the manifest tag of your manifest file matches the package name of your drink class.
I am not sure I understand you correctly, but to go to a different 'page', which is in your case an Activity, you have have to create one yourself.
If you do not see a .java file representing your Activity you should make one like this (note the uppercase Drink for naming conventions):
public class Drink extends Activity{
protected void onCreate(Bundle savedInstance){
this.setContentView(R.layout.drink.xml)
}
}
I am assuming you already made a xml file for the layout of the Activity. Please read the helpfull documentation of Android.
This means that you did not create the class yet. Right - click on your package name in Package Explorer and select Class. Then, create your activity with your desired class name. Also, check if you declared your android:onClick = calculate in your XML file if you get a yellow attention sign that says that your method is not used.

How can i create a New Activity from IntelliJ?

How can i create a new Activity from IntelliJ for Android PRoject, and this automatically generates xml File and .Class
Choose the class from the source folder where you want to create the activity.
Right click.
Choose New > Android Component.
In the New Android Component Dialogue choose Kind > Activity.
Name your Activity and save. You will get a Java source file created that extends Activity and <activity android:name=".view.Sample"/> line in the AndroidManifest.xml.
From the first look, highlight the src folder and click new. currently you have highlighted the layout folder, so it might provide you only those files thats meant to go there.
You can create a java class in src folder which extends the "activity" class.
On OSX you can hit control + n (^N) to come up with the same options!

How to add new activity to existing project in Android Studio?

In Eclipse you just clicked the new button and select the android activity to add new activity. But Android Studio is a bit diferent; I couldn't find out how to add new activity to the project.
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.
In Android Studio 2, just right click on app and select New > Activity > ...
to create desired activity type.
I think natually do it is straightforward, whether Intellij IDEA or Android Studio, I always click new Java class menu, and then typing the class name, press Enter to create.
after that, I manually typing "extends Activity" in the class file, and then import the class by shortcut key.
finally, I also manually override the onCreate() method and invoke the setContentView() method.
In Android Studio, go to app -> src -> main -> java -> com.example.username.projectname
Right click on com.example.username.projectname -> Activity -> ActivityType
Fill in the details of the New Android Activity and click Finish.
Viola! new activity added to the existing project.
In Android Studio, go to app --> src --> main --> res-->
File --> new --> Activity --> ActivityType [choose a acticity that you want]
Fill in the details of the New Android Activity and click Finish.
If you want to see your new Activity's XML representation, you can go to the AndroidManifest.xml file.
You will see your Activity like so:
...
<activity android:name=".MyExerciseActivity"
android:label="My Exercise Chart"
android:parentActivityName=".MainActivity"
/>
<activity android:name=".MainActivity">
...
Add new Activity
Select package -> File -> New -> Activity

Switching Views/layouts

I have a problem that I can't seem to find the solution to.
I have an app that loads the main.xml file on startup, of course. In it are several buttons, and I want the buttons to take me to a different XML file. I just used setContentView(R.layout.newlayout.xml) method for that, and it works great.
The problem comes in after that. If I reference any of the buttons or other objects in the new layout, the app won't even finish loading before it errors out and closes on the emulator. However, if I take all references to objects out, the app runs fine.
I can navigate TO the new layouts, but their buttons can't do anything. Do I need to create a separate Java file for each layout? Or am I doing it all wrong? I'm trying to be as specific as I can. I suppose you could say I need to have different "pages" in my app as a website would.
I think what you are trying to do is best solved using multiple java files, each one defining it's own android Activity.
While it is possible to have multiple layouts/views in a single activity, this will generally make the code more complex and harder to read/debug in the future. By having each 'screen' in its own file, it will be a bit easier to manage all the different views you need to juggle.
The buttons and views only can refer to those mentioned in the current SetContentView() file..
u can test this by creating a button and initialising to an R.id... without setting the content view.. U will get a force close..
so if u change the XML file u shud initialise stuff again....
Ok, for anyone out there with the same problem and haven't figured out how to do it, as I said in my comment on ylebre, my Coworker and I have finally discovered how to do it. First off, we added
implements OnClickListener
to the class, after
extends Activity
then, we created a new java file, and at the beginning of the file it called
setContentView(R.layout.newlayout);
instead of main. Then, we made a button as follows:
Button button1 = (Button) findViewById(R.id.button01;
button1.setOnClickListener(this);
then later in the code:
public void onClick(View v) {
switch(v.getId()) {
case R.id.button01:
startActivity(new Intent(this, NEWJAVAFILE.class));
break;
}
}
And that's it! We just copied and pasted that code into NEWJAVAFILE, changed the names and such, and we were able to navigate freely back and forth. As ylebre said, all of the code for the new activity is in the NEWJAVAFILE.java. OH and don't forget to add the name of the java file to the manifest inside the tags:
<activity android:name=".NEWJAVAFILE">
</activity>
it all seems so simple now!

Categories

Resources