/**
   * <p>Call setup() before adding tabs if loading TabHost using findViewById().
   * <i><b>However</i></b>: You do not need to call setup() after getTabHost()
   * in {@link android.app.TabActivity TabActivity}.
   * Example:</p>
   * <pre>mTabHost = (TabHost)findViewById(R.id.tabhost);
   * mTabHost.setup();
   * mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");
   */
  public void setup() {
    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'");
    }
  }