FileDocCategorySizeDatePackage
TabHost.javaAPI DocAndroid 1.5 API20233Wed May 06 22:41:56 BST 2009android.widget

TabHost

public class TabHost extends FrameLayout implements ViewTreeObserver.OnTouchModeChangeListener
Container for a tabbed window view. This object holds two children: a set of tab labels that the user clicks to select a specific tab, and a FrameLayout object that displays the contents of that page. The individual elements are typically controlled using this container object, rather than setting values on the child elements themselves.

Fields Summary
private TabWidget
mTabWidget
private FrameLayout
mTabContent
private List
mTabSpecs
protected int
mCurrentTab
This field should be made private, so it is hidden from the SDK. {@hide}
private android.view.View
mCurrentView
protected android.app.LocalActivityManager
mLocalActivityManager
This field should be made private, so it is hidden from the SDK. {@hide}
private OnTabChangeListener
mOnTabChangeListener
private OnKeyListener
mTabKeyListener
Constructors Summary
public TabHost(android.content.Context context)


       
        super(context);
        initTabHost();
    
public TabHost(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);
        initTabHost();
    
Methods Summary
public voidaddTab(android.widget.TabHost$TabSpec tabSpec)
Add a tab.

param
tabSpec Specifies how to create the indicator and content.


        if (tabSpec.mIndicatorStrategy == null) {
            throw new IllegalArgumentException("you must specify a way to create the tab indicator.");
        }

        if (tabSpec.mContentStrategy == null) {
            throw new IllegalArgumentException("you must specify a way to create the tab content");
        }
        View tabIndicator = tabSpec.mIndicatorStrategy.createIndicatorView();
        tabIndicator.setOnKeyListener(mTabKeyListener);
        mTabWidget.addView(tabIndicator);
        mTabSpecs.add(tabSpec);

        if (mCurrentTab == -1) {
            setCurrentTab(0);
        }
    
public voidclearAllTabs()
Removes all tabs from the tab widget associated with this tab host.

        mTabWidget.removeAllViews();
        initTabHost();
        mTabContent.removeAllViews();
        mTabSpecs.clear();
        requestLayout();
        invalidate();
    
public booleandispatchKeyEvent(android.view.KeyEvent event)

        final boolean handled = super.dispatchKeyEvent(event);

        // unhandled key ups change focus to tab indicator for embedded activities
        // when there is nothing that will take focus from default focus searching
        if (!handled
                && (event.getAction() == KeyEvent.ACTION_DOWN)
                && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP)
                && (mCurrentView.isRootNamespace())
                && (mCurrentView.hasFocus())
                && (mCurrentView.findFocus().focusSearch(View.FOCUS_UP) == null)) {
            mTabWidget.getChildAt(mCurrentTab).requestFocus();
            playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
            return true;
        }
        return handled;        
    
public voiddispatchWindowFocusChanged(boolean hasFocus)

        mCurrentView.dispatchWindowFocusChanged(hasFocus);
    
public intgetCurrentTab()

        return mCurrentTab;
    
public java.lang.StringgetCurrentTabTag()

        if (mCurrentTab >= 0 && mCurrentTab < mTabSpecs.size()) {
            return mTabSpecs.get(mCurrentTab).getTag();
        }
        return null;
    
public android.view.ViewgetCurrentTabView()

        if (mCurrentTab >= 0 && mCurrentTab < mTabSpecs.size()) {
            return mTabWidget.getChildAt(mCurrentTab);
        }
        return null;
    
public android.view.ViewgetCurrentView()

        return mCurrentView;
    
public FrameLayoutgetTabContentView()
Get the FrameLayout which holds tab content

        return mTabContent;
    
public TabWidgetgetTabWidget()

        return mTabWidget;
    
private final voidinitTabHost()

        setFocusableInTouchMode(true);
        setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);

        mCurrentTab = -1;
        mCurrentView = null;
    
private voidinvokeOnTabChangeListener()

        if (mOnTabChangeListener != null) {
            mOnTabChangeListener.onTabChanged(getCurrentTabTag());
        }
    
public android.widget.TabHost$TabSpecnewTabSpec(java.lang.String tag)
Get a new {@link TabSpec} associated with this tab host.

param
tag required tag of tab.

        return new TabSpec(tag);
    
protected voidonAttachedToWindow()

        super.onAttachedToWindow();
        final ViewTreeObserver treeObserver = getViewTreeObserver();
        if (treeObserver != null) {
            treeObserver.addOnTouchModeChangeListener(this);
        }
    
protected voidonDetachedFromWindow()

        super.onDetachedFromWindow();
        final ViewTreeObserver treeObserver = getViewTreeObserver();
        if (treeObserver != null) {
            treeObserver.removeOnTouchModeChangeListener(this);
        }
    
public voidonTouchModeChanged(boolean isInTouchMode)
{@inheritDoc}

        if (!isInTouchMode) {
            // leaving touch mode.. if nothing has focus, let's give it to
            // the indicator of the current tab
            if (!mCurrentView.hasFocus() || mCurrentView.isFocused()) {
                mTabWidget.getChildAt(mCurrentTab).requestFocus();
            }
        }
    
public voidsetCurrentTab(int index)

        if (index < 0 || index >= mTabSpecs.size()) {
            return;
        }

        if (index == mCurrentTab) {
            return;
        }

        // notify old tab content
        if (mCurrentTab != -1) {
            mTabSpecs.get(mCurrentTab).mContentStrategy.tabClosed();
        }

        mCurrentTab = index;
        final TabHost.TabSpec spec = mTabSpecs.get(index);

        // Call the tab widget's focusCurrentTab(), instead of just
        // selecting the tab.
        mTabWidget.focusCurrentTab(mCurrentTab);
        
        // tab content
        mCurrentView = spec.mContentStrategy.getContentView();

        if (mCurrentView.getParent() == null) {
            mTabContent
                    .addView(
                            mCurrentView,
                            new ViewGroup.LayoutParams(
                                    ViewGroup.LayoutParams.FILL_PARENT,
                                    ViewGroup.LayoutParams.FILL_PARENT));
        }

        if (!mTabWidget.hasFocus()) {
            // if the tab widget didn't take focus (likely because we're in touch mode)
            // give the current tab content view a shot
            mCurrentView.requestFocus();
        }

        //mTabContent.requestFocus(View.FOCUS_FORWARD);
        invokeOnTabChangeListener();
    
public voidsetCurrentTabByTag(java.lang.String tag)

        int i;
        for (i = 0; i < mTabSpecs.size(); i++) {
            if (mTabSpecs.get(i).getTag().equals(tag)) {
                setCurrentTab(i);
                break;
            }
        }
    
public voidsetOnTabChangedListener(android.widget.TabHost$OnTabChangeListener l)
Register a callback to be invoked when the selected state of any of the items in this list changes

param
l The callback that will run

        mOnTabChangeListener = l;
    
public voidsetup()

Call setup() before adding tabs if loading TabHost using findViewById(). However: You do not need to call setup() after getTabHost() in {@link android.app.TabActivity TabActivity}. Example:

mTabHost = (TabHost)findViewById(R.id.tabhost);
mTabHost.setup();
mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");

        mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
        if (mTabWidget == null) {
            throw new RuntimeException(
                    "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
        }
        
        // KeyListener to attach to all tabs. Detects non-navigation keys 
        // and relays them to the tab content.
        mTabKeyListener = new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_DPAD_LEFT:
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                    case KeyEvent.KEYCODE_DPAD_UP:
                    case KeyEvent.KEYCODE_DPAD_DOWN:
                    case KeyEvent.KEYCODE_ENTER:
                        return false;
                    
                }
                mTabContent.requestFocus(View.FOCUS_FORWARD);
                return mTabContent.dispatchKeyEvent(event);
            }
            
        };
        
        mTabWidget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() {
            public void onTabSelectionChanged(int tabIndex, boolean clicked) {
                setCurrentTab(tabIndex);
                if (clicked) {
                    mTabContent.requestFocus(View.FOCUS_FORWARD);
                }
            }
        });

        mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
        if (mTabContent == null) {
            throw new RuntimeException(
                    "Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'");
        }
    
public voidsetup(android.app.LocalActivityManager activityGroup)
If you are using {@link TabSpec#setContent(android.content.Intent)}, this must be called since the activityGroup is needed to launch the local activity. This is done for you if you extend {@link android.app.TabActivity}.

param
activityGroup Used to launch activities for tab content.

        setup();
        mLocalActivityManager = activityGroup;