Marcos Roriz Junior
Marcos Roriz Junior

Reputation: 4106

Problem with huge android layout

I just started with Android and I'm doing a simple app that I want to look like this:

-------------------------------------------
|             |             |             |   
|   T  A  B   |   T  A  B   |   T  A  B   |
|  SELECTED   |             |             |   
|-----------------------------------------|
|                                         |
| VIEW FLIP CONTENT                       |
|                                         |
|                                         |
|                                         |
|                                         |
|                                         |
|                                         |
|                                         |
|                                         |
-------------------------------------------

That is, I have some tabs and each one has a view flip component. Which enables them to have internal "tabs". The problem is that with this configuration I have to run everything on a single Activity! Also, my layout is getting huge (see image attached). How can I solve this mess? Run each tab as a single activity?

layout getting huge

Upvotes: 1

Views: 272

Answers (1)

jorgenfb
jorgenfb

Reputation: 2237

It is possible to put all your tabs in different Activities. Create simple layout for your parent Activity, something like this:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />    
    </LinearLayout>
</TabHost>

Now you have created the tab layout you want, now its time to fill your tabs with the Activities you want. This is done in the onCreate method of you TabActivity.

public class MyTabActivity extends TabActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, YourFirstActivity.class);


        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("NameOfSpec").setIndicator("TabText1"),null).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, YourSecondActivity.class);

        spec = tabHost.newTabSpec("NameOfSpec2").setIndicator("TabText2",null).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
    }
}

This is it, now you have a tabhost with 2 tabs in it, to add more just keep adding more tabhosts. And ofcourse you need to change the text for each tab, and fill in your own activities instead of "YourFirstActivity" and "YourSecondActivity".

For a tutorial, take a look here: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

Upvotes: 1

Related Questions