Escaga Seen
Escaga Seen

Reputation: 151

How to add an already created activity to my project?

I'm creating an App, with some classmates, and we want to share our Activities with others, so we don't have to do all over again. Is that possible?

Upvotes: 0

Views: 56

Answers (2)

Albert Lazaro de Lara
Albert Lazaro de Lara

Reputation: 2710

You can copy the activity class and the XML layout of the activity from one project to another.

The activity class file goes in the source folder and the xml layout goes in the layouts folder.

To declare the activity in the manifest.xml you have to add:

<activity
     android:name="com.example.stockquote.StockInfoActivity"
     android:label="@string/app_name" />

with the correct name and label (You have to declare in manifest, otherwise it won't work).

You can try to open other activities from main activity with:

Intent myIntent = new Intent(this, MyActivityName.class);
startActivity(myIntent);

In that case, you can add a Button to open the new activities and test if it works:

Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(this, MyActivityName.class);
                startActivity(myIntent);
            }
});

Upvotes: 2

nir
nir

Reputation: 312

You can copy the activity class into the Java folder in your project and the XML layout of the activity from one project to another, paste it in the layout folder (under "res").

Remember to fix the package name, and define the activity in the manifest! <activity android:name=".the.right.path.ActivityName"/>

you should enter a path according to the package structure in your project

good luck

Upvotes: 0

Related Questions