0

I am writing a code in Android to create Horizontal and Vertical tabs. In my code , i only able to create Horizontal tab , which is default.

XML Code

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cccccc"
    android:padding="5dp">



   <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#cccccc"
            android:padding="5dp">

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

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#ffffff"
                    android:padding="5dp">            

                            <GridView 
                                android:id="@+id/gridView1"
                                android:numColumns="10"
                                android:gravity="center"
                                android:layout_marginLeft="0dp"
                                android:layout_marginTop="0dp"
                                android:stretchMode="columnWidth"
                                android:paddingLeft="3dp"
                                android:paddingRight="3dp"
                                android:horizontalSpacing="3dp"
                                android:verticalSpacing="3dp" 
                                android:layout_width="1600dp"
                                android:layout_height="fill_parent"/>

        </LinearLayout>            
                     </LinearLayout> 

              </HorizontalScrollView>

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

                    </LinearLayout>

                  </TabHost>

Java Code :

         package com.example.tabsabc;

         import java.util.ArrayList;

         import android.os.Bundle;
         import android.app.ActionBar;
         import android.app.Activity;
         import android.app.Fragment;
         import android.app.TabActivity;
         import android.view.Menu;
         import android.view.View;
         import android.widget.ArrayAdapter;
         import android.widget.GridView; 
         import android.widget.TabHost;
         import android.widget.TabWidget;
         import android.widget.TextView;

         public class MainActivity extends TabActivity implements TabHost.TabContentFactory {


ArrayList<String> names;

GridView gd;

BoxGrid gdv;

TabWidget tb1;

TabWidget tb2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    gd = (GridView)findViewById(R.id.gridView1);

    //tb1 = (TabWidget)findViewById(R.id.tabs1);

    names = new ArrayList<String>();

    final TabHost tabHost = getTabHost();

    for (int i=1; i <= 10; i++) {
        String name = "Tab " + i;
        tabHost.addTab(tabHost.newTabSpec(name)
                .setIndicator(name)
                .setContent(this));



    }

    for(int i1=0;i1<150;i1++)
    {

        names.add(String.valueOf(i1));

    }

     ArrayAdapter gdv = new ArrayAdapter(this, android.R.layout.simple_list_item_1, names);

     gd.setAdapter(gdv);

}

/** {@inheritDoc} */
public View createTabContent(String tag) {
    final TextView tv = new TextView(this);
    tv.setText("Content for tab with tag " + tag);
    return tv;
}}

So, this is my above code and i can't find the place where can i add the vertical tab in the XML layout. I don't find any technique to do this.

Please let me know , suggest me some good solution.

user2563306
  • 113
  • 1
  • 2
  • 8

1 Answers1

0

I'll leave this here, because that's a more comfortable way than trying to style the TabWidget:

Looking for a universal TabHost style that will work on Android, HTC Sense, Samsung, etc. skins

But even better, use Fragments inside a ViewPager and not the old TabHost + TabWidget approach. It's the recommended way of doing Tabs in Android nowadays anyway.

Community
  • 1
  • 1
einschnaehkeee
  • 1,858
  • 2
  • 17
  • 20
  • Yes thanks , but idea is not getting clear to me , only i can understand that we can use ActionBar Sherlock API to create Tabs.... can you please give some specific link ? so that i can reach their quickly.... – user2563306 May 14 '14 at 10:10
  • try using my answer provided by the link, you don't need to use ABS, if you want to keep using TabHost+TabWidget, just follow the instructions. Here is also a complete example project: http://einschnaehkeee.blogspot.de/ – einschnaehkeee May 14 '14 at 10:17
  • But keep in mind, that Fragments are more supported and easier to handle than TabHost+TabWidget – einschnaehkeee May 14 '14 at 10:19