Search code examples
androidandroid-layoutandroid-fragmentsandroid-tabhostandroid-tabs

How to set a default page for tab host in android?


The behavior of tabhost seems by default will auto select the first tab and show the content of it. The problem is , I would like to set a default page of it. (That means, by default the tabhost is not selected , and showing some home page content, (not the content of tabhost)). How to implement such function ? Thanks.

Main Activity

public class LandingPage extends FragmentActivity {
    public FragmentTabHost tabHost;
    public Resources res;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.landing_page);
        res = getResources();
        tabHost = (FragmentTabHost) findViewById(R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);  
        tabHost.addTab(tabHost.newTabSpec("About").setIndicator("",res.getDrawable(R.drawable.btn_about)),About.class, null);
        tabHost.addTab(tabHost.newTabSpec("Selfie").setIndicator("",res.getDrawable(R.drawable.btn_selfie)),About.class, null);
        tabHost.addTab(tabHost.newTabSpec("Gallery").setIndicator("",res.getDrawable(R.drawable.btn_gallery)),About.class, null);
        tabHost.addTab(tabHost.newTabSpec("LeaderBoard").setIndicator("",res.getDrawable(R.drawable.btn_board)),About.class, null);
        tabHost.addTab(tabHost.newTabSpec("Events").setIndicator("",res.getDrawable(R.drawable.btn_events)),Events.class, null);
    }
}

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.app.FragmentTabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal"
        android:background="@drawable/share_navigation_bar_bg" />

    <FrameLayout
        android:id="@+id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

Solution

  • I fixed it. Add one more tab and set the visiblity of it is View.gone