0

I'm creating android app that has table layout for the main activity, and that part works perfectly... Now, the idea was to add another part of an app below the existing components, but now I have to put a tabbed layout there. Well, that part also works perfectly when I try to run just that. But what do I have to do to mix those two in such a way that these two show up one below another on the very same screen.

My main code is:

package my.android;

import android.os.Bundle;

public class MyActivity extends FragmentActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

I have different layout files for all the tabs and I have my TabsActivity class I have created following the tutorial here: http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

So how do I add some TabsActivity ta object to the MyActivity? And it is important to be below the content of this. Thaks in advance...

1 Answers1

0

Ideally this would be done using nested Fragments, but Android doesn't support that yet. That leaves the deprecated ActivityGroup class. You'll need a top level activity that extends ActivityGroup and launches the two activities.

Here is how you launch the activities and get their views:

final Window w = getLocalActivityManager().startActivity(myTag, myIntent);
final View wd = w != null ? w.getDecorView() : null;
if (  null != wd ) {
    wd.setVisibility(View.VISIBLE);
    wd.setFocusableInTouchMode(true);
}
// TODO: Attach wd to a ViewGroup.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Edit: Below is a more complete solution.

This is the layout for the top level activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    > 
</LinearLayout>

Here is the top level class:

public class EmbeddedActivityParent extends ActivityGroup {

    private LinearLayout    mRootLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         mRootLayout = (LinearLayout) findViewById(R.id.root_layout);

         // Add embedded status activity.
         embedActivity("StatusColumn", new Intent(this, StatusActivity.class));

         // Add embedded work activity.
         embedActivity("WorkArea", new Intent(this, MainActivity.class));
    }

    private void embedActivity(String myTag, Intent launchIntent) {
         final Window w = getLocalActivityManager().startActivity(myTag, launchIntent);
         final View wd = w != null ? w.getDecorView() : null;
         if (  null != wd ) {
             wd.setVisibility(View.VISIBLE);
             wd.setFocusableInTouchMode(true);

             mRootLayout.addView(wd);
         }
    }
}

You can add as many embedded activities as you want. You can even nest embedded activities, but be aware that performance could become a factor. We use this to support a dynamic status column.

Personally, I think there is still a use for the ActivityGroup and hope that Gooogle changes their mind about deprecating it.

jsmith
  • 4,847
  • 2
  • 32
  • 39
  • So, you mean, on top level there is a TopLevelActivity that extends the ActivityGroup, and has in the onCreate method the code you've written above. If I have two activities that are named Act1 and Act2, with their own Views, how do I put all of them together? I don't get your point yet, please can you write some more example code on this topic? Thanks in advance. – Adriano Maljkovic Nov 02 '11 at 19:00
  • See the modified response above. – jsmith Dec 01 '11 at 15:48