Tuesday 8 January 2013

Tab with scroll

Basically when we are using Tab-bar with number of tabs then all the tabs show in one screen not identify for particular tab click, can't scroll it.Here i implement  Tab-bar with horizontal scroll by using HorizontalScrollView in tab-host as show in XML file,In below code i added  tab as dynamically on button click.And easily control tabs.

public class MyTabActivity extends TabActivity {
private static int tabIx = 0;
private TabHost tabHost;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
tabHost = getTabHost();

addTab();

((Button) findViewById(R.id.add_tab))
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tabIx++;
addTab();
}
});
}

private void addTab() {
LayoutInflater layoutInflate = LayoutInflater.from(MyTabActivity.this);

Button tabBtn = (Button) layoutInflate.inflate(R.layout.newtab_event, null);
tabBtn.setText("Tab " + tabIx);
Intent tabIntent = new Intent(TabActivity.this, NewActivity.class);
setupTab(tabBtn, tabIntent, "Tab " + tabIx);
}

protected void setupTab(View tabBtn, Intent setClass, String tag) {
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabBtn)
.setContent(setClass);
tabHost.addTab(setContent);
}
}

activity_tab.xml file

<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" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="5dp" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <HorizontalScrollView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:fillViewport="true"
                android:scrollbars="none" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="0dip"
                    android:layout_marginRight="0dip" />
            </HorizontalScrollView>

            <Button
                android:id="@+id/add_tab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.1"
                android:text="Add" />
        </LinearLayout>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="2dp" />
    </LinearLayout>

</TabHost>

No comments:

Post a Comment

Copy and share your useful data from Google chrome browser to your application in android.

Here In this blog I explain that how to copy and share your useful data from chrome browser in android. 1) How to show your applic...