0

After adding tabhost and tabcontent can't create tabview. How to add tab in android. I use below code to display tabs.

XML File:

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

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/homeLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <LinearLayout
                        android:id="@+id/dynamicLinearLayout"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical" >

                        <Button
                            android:id="@+id/button1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Dynamic" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/staticLinearLayout"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical" >

                        <Button
                            android:id="@+id/button2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Static" />
                    </LinearLayout>
                </FrameLayout>
            </TabWidget>
        </LinearLayout>
    </TabHost>

</LinearLayout>

Java File

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_view);
    tabHost = (TabHost) findViewById(android.R.id.tabhost);
    tabHost.setup();

    tabSpec = tabHost.newTabSpec("dynamicTab");
    tabSpec.setIndicator("Create Image");
    tabSpec.setContent(R.id.dynamicLinearLayout);
    tabHost.addTab(tabSpec);

    tabSpec = tabHost.newTabSpec("staticTab");
    tabSpec.setIndicator("Select Image");
    tabSpec.setContent(R.id.staticLinearLayout);
    tabHost.addTab(tabSpec);

    tabHost.setCurrentTab(0);
}

Output is: enter image description here

Can't load two tabs. Please show where I make a mistake.?

ligi
  • 39,001
  • 44
  • 144
  • 244
Samir Savasani
  • 352
  • 3
  • 8
  • u need to give a different name to tabhost from the different previous one like this tabSpec2 – sakir Sep 28 '14 at 12:48

2 Answers2

0
tabSpec = tabHost.newTabSpec("dynamicTab");
    tabSpec.setIndicator("Create Image");
    tabSpec.setContent(R.id.dynamicLinearLayout);
    tabHost.addTab(tabSpec);

    tabSpec2 = tabHost.newTabSpec("staticTab");
    tabSpec2.setIndicator("Select Image");
    tabSpec2.setContent(R.id.staticLinearLayout);
    tabHost2.addTab(tabSpec2);

    tabHost.setCurrentTab(0);
sakir
  • 3,391
  • 8
  • 34
  • 50
0

for java part the answer of sakir is correct you can use it or use mine:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
    tabHost.setup();

    TabHost.TabSpec tabSpec = tabHost.newTabSpec("dynamicTab");
    tabSpec.setIndicator("Create Image");
    tabSpec.setContent(R.id.dynamicLinearLayout);
    tabHost.addTab(tabSpec);

    TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("staticTab");
    tabSpec1.setIndicator("Select Image");
    tabSpec1.setContent(R.id.staticLinearLayout);
    tabHost.addTab(tabSpec1);

    tabHost.setCurrentTab(0); 

for your xml part you must remove </TabWidget> and change:

      <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

to

       <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
mmlooloo
  • 18,937
  • 5
  • 45
  • 64