Question

I am designing tabs in my app. I am confused whether to use activities for tab or just use views

which one is the better way for tablayout

I am confused ..

I am bit new to tabs and still learning tab layout in android, so my question can be a little naive so please bear with it :)

Was it helpful?

Solution

To answer your question of "activity or view in a tab layout"

This is what the android tutorial sais:

You can implement your tab content in one of two ways: use the tabs to swap Views within the same Activity, or use the tabs to change between entirely separate activities. Which method you want for your application will depend on your demands, but if each tab provides a distinct user activity, then it probably makes sense to use a separate Activity for each tab, so that you can better manage the application in discrete groups, rather than one massive application and layout. Android tablayout tutorial

I highly recommend following the tutorial, then try to implement your own version for your app.

Here is a code sample just in case;

public class TabHostExample extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testtabs); 

    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent content;

    spec = tabHost.newTabSpec("test1"); //set a title for the tab
    spec.setIndicator("test1"),
            getResources().getDrawable(R.drawable.ic_dialog_alert)); //set an icon for tab
    content = new Intent(this, ExampleActivityOne.class);
    spec.setContent(content);
    tabHost.addTab(spec);

    spec = tabHost.newTabSpec("test2")); //set a title for the tab
    spec.setIndicator("test2"),
            getResources().getDrawable(R.drawable.ic_dialog_info)); //set an icon for the tab
    content = new Intent(this, ExampleActivityTwo.class);
    spec.setContent(content);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

}

    <TabHost
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost">
    <LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    >   
        <TabWidget
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/tabs" 
        />
        <FrameLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@android:id/tabcontent" 
        />
    </LinearLayout>
</TabHost>

A bit of extra information from the tutorial:

The framelayout is where the content of the activity per tab goes.

Notice that the TabWidget and the FrameLayout elements have the IDs tabs and tabcontent, respectively. These names must be used so that the TabHost can retrieve references to each of them. It expects exactly these names.

So with this code you have a basic setup for a tab layout, what you do next is, attach an activity to a tab. You use the activity like normal, for these activities you extend Activity as you normally would for an activity and build your layout in onCreate/onResume

If you need more information or more explanation please leave a comment. I think most code is self explanatory though.

I just noticed my code is almost an exact copy from the tutorial page. I actually use this code in my app, with different names for the activities etc. It works great. I recommend it. Credits goes to the Android team.

OTHER TIPS

You have to use activities. Every tab has activity. There are good tutorials at developer.android.com Try this one here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top