2

I have created 4 tabs in my application using tabHost , it working fine. below i show my code for adding only 2 tabs.

           public class Home_tab extends TabActivity {


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_main);

    Resources ressources = getResources(); 
    TabHost tabHost = getTabHost(); 

    // Android tab

    Intent intentAndroid = new Intent().setClass(this, CoalActivity.class);
    TabSpec tabSpecAndroid = tabHost


      .newTabSpec("Android")

      .setIndicator("", ressources.getDrawable(R.drawable.tab_dis))
      .setContent(intentAndroid);

    // Apple tab
    Intent intentApple = new Intent().setClass(this, EnergyActivity.class);
    TabSpec tabSpecApple = tabHost
      .newTabSpec("Apple")

      .setIndicator("", ressources.getDrawable(R.drawable.tab_foc))
      .setContent(intentApple);


    // add all tabs 
    tabHost.addTab(tabSpecAndroid);
    tabHost.addTab(tabSpecApple);

                }
              }

This is my xml file

       <?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"
         android:background="@drawable/background"
         android:scrollbarAlwaysDrawHorizontalTrack="true"
        >
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp" />
  </LinearLayout>
  </TabHost>

But my problem is when i add more tabs, it comes in the same window. for example, currently i added 4 tabs, when i try to add 3 more tabs , all the tabs are coming in the same window? i want to add only 4 tabs in the same window, other tabs should appears only when i scroll the tab bar How to solve this??

SibinF
  • 385
  • 1
  • 7
  • 25
  • 1
    there're good reasons why the TabHost is deprecated. It doesn't work very well, it's not flexible enough, it's difficult to implement. On this link you can see how easy is to implement tabs in the actionbar: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs – Budius Mar 06 '13 at 09:34
  • if anybody can solve my problem please help me......... my tab is working fine, but only the problem is every tab is added to the same window? ? – SibinF Mar 06 '13 at 09:48

1 Answers1

1

Check following links and let me know if they helps in solving your problem

Link1

Link2

link3

Community
  • 1
  • 1
Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
  • Hi Abhinav Singh Thanx very much – SibinF Mar 06 '13 at 10:05
  • hi now my tabs can be make scrollable but now i have a problem how to adjust the size of each tabs?, – SibinF Mar 06 '13 at 10:22
  • hi Abhinav , i added 2 images, in the first image it shows only 3 tabs and when i scroll horizontally the 4th, 5th and 6th tab display completly in the screen?? how to solve this – SibinF Mar 06 '13 at 10:50
  • Check this link out for help http://stackoverflow.com/questions/5799320/android-remove-space-between-tabs-in-tabwidget?rq=1 You can give tabs width also like tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 140; – Abhinav Singh Maurya Mar 06 '13 at 11:01
  • check this link also http://stackoverflow.com/questions/6453906/how-to-change-size-of-tabs – Abhinav Singh Maurya Mar 06 '13 at 11:04
  • hi when i add tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 140; it showing NullPointer Exception .what to do .please give me a solution.. – SibinF Mar 06 '13 at 11:06
  • http://stackoverflow.com/questions/6453906/how-to-change-size-of-tabs i tried this , but in the int width = display.getWidth(); it showing NullPointer exception – SibinF Mar 06 '13 at 11:09
  • post another question and clearly specify your problem in that question and let people solve your problem. – Abhinav Singh Maurya Mar 06 '13 at 11:20