FileDocCategorySizeDatePackage
TabWidget.javaAPI DocAndroid 1.5 API10167Wed May 06 22:41:56 BST 2009android.widget

TabWidget

public class TabWidget extends LinearLayout implements android.view.View.OnFocusChangeListener
Displays a list of tab labels representing each page in the parent's tab collection. The container object for this widget is {@link android.widget.TabHost TabHost}. When the user selects a tab, this object sends a message to the parent container, TabHost, to tell it to switch the displayed page. You typically won't use many methods directly on this object. The container TabHost is used to add labels, add the callback handler, and manage callbacks. You might call this object to iterate the list of tabs, or to tweak the layout of the tab list, but most methods should be called on the containing TabHost object.

Fields Summary
private OnTabSelectionChanged
mSelectionChangedListener
private int
mSelectedTab
private android.graphics.drawable.Drawable
mBottomLeftStrip
private android.graphics.drawable.Drawable
mBottomRightStrip
private boolean
mStripMoved
Constructors Summary
public TabWidget(android.content.Context context)


       
        this(context, null);
    
public TabWidget(android.content.Context context, android.util.AttributeSet attrs)

        this(context, attrs, com.android.internal.R.attr.tabWidgetStyle);
    
public TabWidget(android.content.Context context, android.util.AttributeSet attrs, int defStyle)

        super(context, attrs);
        initTabWidget();

        TypedArray a = 
            context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TabWidget,
                    defStyle, 0);

        a.recycle();
    
Methods Summary
public voidaddView(android.view.View child)

        if (child.getLayoutParams() == null) {
            final LinearLayout.LayoutParams lp = new LayoutParams(
                    0,
                    ViewGroup.LayoutParams.WRAP_CONTENT, 1);
            lp.setMargins(0, 0, 0, 0);
            child.setLayoutParams(lp);
        }

        // Ensure you can navigate to the tab with the keyboard, and you can touch it
        child.setFocusable(true);
        child.setClickable(true);

        super.addView(child);

        // TODO: detect this via geometry with a tabwidget listener rather
        // than potentially interfere with the view's listener
        child.setOnClickListener(new TabClickListener(getChildCount() - 1));
        child.setOnFocusChangeListener(this);
    
public voidchildDrawableStateChanged(android.view.View child)

        if (child == getChildAt(mSelectedTab)) {
            // To make sure that the bottom strip is redrawn
            invalidate();
        }
        super.childDrawableStateChanged(child);
    
public voiddispatchDraw(android.graphics.Canvas canvas)

        super.dispatchDraw(canvas);

        View selectedChild = getChildAt(mSelectedTab);
        
        mBottomLeftStrip.setState(selectedChild.getDrawableState());
        mBottomRightStrip.setState(selectedChild.getDrawableState());
        
        if (mStripMoved) {
            Rect selBounds = new Rect(); // Bounds of the selected tab indicator
            selBounds.left = selectedChild.getLeft();
            selBounds.right = selectedChild.getRight();
            final int myHeight = getHeight();
            mBottomLeftStrip.setBounds(
                    Math.min(0, selBounds.left 
                                 - mBottomLeftStrip.getIntrinsicWidth()),
                    myHeight - mBottomLeftStrip.getIntrinsicHeight(),
                    selBounds.left,
                    getHeight());
            mBottomRightStrip.setBounds(
                    selBounds.right,
                    myHeight - mBottomRightStrip.getIntrinsicHeight(),
                    Math.max(getWidth(), 
                            selBounds.right + mBottomRightStrip.getIntrinsicWidth()),
                    myHeight);
            mStripMoved = false;
        }
        
        mBottomLeftStrip.draw(canvas);
        mBottomRightStrip.draw(canvas);
    
public voidfocusCurrentTab(int index)
Sets the current tab and focuses the UI on it. This method makes sure that the focused tab matches the selected tab, normally at {@link #setCurrentTab}. Normally this would not be an issue if we go through the UI, since the UI is responsible for calling TabWidget.onFocusChanged(), but in the case where we are selecting the tab programmatically, we'll need to make sure focus keeps up.

param
index The tab that you want focused (highlighted in orange) and selected (tab brought to the front of the widget)
see
#setCurrentTab

        final int oldTab = mSelectedTab;

        // set the tab
        setCurrentTab(index);
        
        // change the focus if applicable.
        if (oldTab != index) {
            getChildAt(index).requestFocus();
        }
    
private voidinitTabWidget()

        setOrientation(LinearLayout.HORIZONTAL);
        mBottomLeftStrip = mContext.getResources().getDrawable(
                com.android.internal.R.drawable.tab_bottom_left);
        mBottomRightStrip = mContext.getResources().getDrawable(
                com.android.internal.R.drawable.tab_bottom_right);
        // Deal with focus, as we don't want the focus to go by default
        // to a tab other than the current tab
        setFocusable(true);
        setOnFocusChangeListener(this);
    
public voidonFocusChange(android.view.View v, boolean hasFocus)

        if (v == this && hasFocus) {
            getChildAt(mSelectedTab).requestFocus();
            return;
        }
        
        if (hasFocus) {
            int i = 0;
            while (i < getChildCount()) {
                if (getChildAt(i) == v) {
                    setCurrentTab(i);
                    mSelectionChangedListener.onTabSelectionChanged(i, false);
                    break;
                }
                i++;
            }
        }
    
protected voidonSizeChanged(int w, int h, int oldw, int oldh)

        mStripMoved = true;
        super.onSizeChanged(w, h, oldw, oldh);
    
public voidsetCurrentTab(int index)
Sets the current tab. This method is used to bring a tab to the front of the Widget, and is used to post to the rest of the UI that a different tab has been brought to the foreground. Note, this is separate from the traditional "focus" that is employed from the view logic. For instance, if we have a list in a tabbed view, a user may be navigating up and down the list, moving the UI focus (orange highlighting) through the list items. The cursor movement does not effect the "selected" tab though, because what is being scrolled through is all on the same tab. The selected tab only changes when we navigate between tabs (moving from the list view to the next tabbed view, in this example). To move both the focus AND the selected tab at once, please use {@link #setCurrentTab}. Normally, the view logic takes care of adjusting the focus, so unless you're circumventing the UI, you'll probably just focus your interest here.

param
index The tab that you want to indicate as the selected tab (tab brought to the front of the widget)
see
#focusCurrentTab

        if (index < 0 || index >= getChildCount()) {
            return;
        }

        getChildAt(mSelectedTab).setSelected(false);
        mSelectedTab = index;
        getChildAt(mSelectedTab).setSelected(true);
        mStripMoved = true;
    
public voidsetEnabled(boolean enabled)

        super.setEnabled(enabled);
        int count = getChildCount();
        
        for (int i=0; i<count; i++) {
            View child = getChildAt(i);
            child.setEnabled(enabled);
        }
    
voidsetTabSelectionListener(android.widget.TabWidget$OnTabSelectionChanged listener)
Provides a way for {@link TabHost} to be notified that the user clicked on a tab indicator.

        mSelectionChangedListener = listener;