简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM