Wednesday, February 13, 2013

Android Scrollable Tabs

we can make tab widgets scrollable. to achieve this trick we just need to follow few steps give in the code below here.

MainActivity.java


package com.jiteshscrollabletabs;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

/**
 * Demonstrates the Tab scrolling when too many tabs are displayed to fit in the
 * screen.
 */
public class MainActivity extends TabActivity implements
TabHost.TabContentFactory {

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

setContentView(R.layout.activity_main);

final TabHost tabHost = getTabHost();

for (int i = 1; i <= 30; i++) {
String name = "Tab data" + i + " ";
tabHost.addTab(tabHost.newTabSpec(name).setIndicator(name)
.setContent(this));
tabHost.getTabWidget().setDividerDrawable(R.drawable.ic_launcher);
}
}

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


main.xml

<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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#ff000066"
                android:orientation="horizontal" >
            </TabWidget>
        </HorizontalScrollView>

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

</TabHost>

the output is shown below


4 comments:

  1. Excellent post about android developing apps. This is the forum for help and discussion on Android Application Development. Get you an idea of how to start developing. Android applications.Introduce major Android application concepts.

    Android Application Development

    ReplyDelete
  2. Hi Aren , i can help you to start the application development and i can send you tutorials and the books where you can follow concepts and can learn the development of android applications, have fun, please ask without any hesitation, will be present here for yours help!!

    ReplyDelete
  3. How do i assign different activities to different tabs?

    ReplyDelete
  4. hi vikram it is easier to understand. please follow the below code tricks

    mport android.app.TabActivity;
    import android.content.Intent;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.widget.TabHost;
    import android.widget.TabHost.TabSpec;

    public class MainActivity extends TabActivity {

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

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

    // Android tab
    Intent intentAndroid = new Intent().setClass(this, AndroidActivity.class);
    TabSpec tabSpecAndroid = tabHost
    .newTabSpec("Android")
    .setIndicator("", ressources.getDrawable(R.drawable.icon_android_config))
    .setContent(intentAndroid);

    // Apple tab
    Intent intentApple = new Intent().setClass(this, AppleActivity.class);
    TabSpec tabSpecApple = tabHost
    .newTabSpec("Apple")
    .setIndicator("", ressources.getDrawable(R.drawable.icon_apple_config))
    .setContent(intentApple);

    // Windows tab
    Intent intentWindows = new Intent().setClass(this, WindowsActivity.class);
    TabSpec tabSpecWindows = tabHost
    .newTabSpec("Windows")
    .setIndicator("", ressources.getDrawable(R.drawable.icon_windows_config))
    .setContent(intentWindows);

    // Blackberry tab
    Intent intentBerry = new Intent().setClass(this, BlackBerryActivity.class);
    TabSpec tabSpecBerry = tabHost
    .newTabSpec("Berry")
    .setIndicator("", ressources.getDrawable(R.drawable.icon_blackberry_config))
    .setContent(intentBerry);

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

    //set Windows tab as default (zero based)
    tabHost.setCurrentTab(2);
    }

    }

    ReplyDelete