Hong
Hong

Reputation: 18521

Can a new Android activity be created using an existing layout?

It seems that a new layout must be created when a new Android activity is created with the wizard in Eclipse. Whenever I create a new Android activity using an existing layout, I have to create a dummy layout, change the layout in onCreate() with setContentView(), then delete the dummy layout.

What is the best way to do this?

Upvotes: 1

Views: 2193

Answers (2)

doubleA
doubleA

Reputation: 2456

You are relying too much on eclipse wizards. Be a programmer. Right click on package add new "class" Give it a name. Extend Activity. Override onCreate methods. In set content view use the layout already created.

Edit: Here are exact instructions

Right click on you package. Click New. Select Class.

Give your class a name, click Ok.

package com.example.fakeapp;

public class FakeActivity {

}

Now extend Activity add in onCreate and onCreateOptionsMenu Use the layout that you need in set content view.

package com.example.fakeapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class FakeActivity extends Activity{
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other_activity); //use whatever layout you want.
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Add to the manifest between the tags dont forget to create your title in your res/strings.

    <activity
        android:name="com.example.fakeapp.Fakeactivity"
        android:label="@string/title_activity_fakeactivity" >
    </activity>

I did not mean to sound arrogant. What i meant to say was learn what the wizard is doing so you can recreate it and not rely on it to do everything for you. If you are afraid of editing the manifest then that is something that you need to learn.

Upvotes: 0

ian
ian

Reputation: 303

Edited Post: If you click File > New > Other, you can choose "Android > Android Activity". Click next, and fill in the right data. If you reach the "Preview" part, you can select the changes that must be performed. I called the new activity "SecondActivity", which means the layout would file would be called "second_activity.xml". If you uncheck this file in the list, it won't create this file. Then just change your setContentView to the file you want.

Upvotes: 1

Related Questions