0

I have an android application that currently has 9 tabs. Whenever a new tab is added, the widths of all the tabs are adjusted smaller. I feel that adding another tab will make the tabs too small. Is there a way to have a right/left arrow appear on the side of the TabWidget, similar to .NET tab controls, or would it be better to wrap a TabWidget in a HorizontalScrollView as described here? scrolling tabwidget

Community
  • 1
  • 1
Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176
  • Just a note, remember not every device has the same physical width, so 9 tabs might be reasonable on one device, might be horrible on another, and might be amazing on a tablet. – Mike dg Apr 28 '11 at 12:23

2 Answers2

6

Simply putting TabWidget in a HorizontalScrollView will meet a problem:
View in TabWidget will not fill the screen and have different width.
You can avoid it by setting its minWidth and set the HorizontalScrollView's android:fillViewport as true.

<HorizontalScrollView ... android:fillViewport="true">  
    <TabWidget android:id="@android:id/tabs" ... />
</HorizontalScrollView>

and

int dips = 70; // The min width you want in DIP
int pixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) dips, getResources().getDisplayMetrics());
    TabWidget widget = tabHost.getTabWidget();
    for (int i = 0; i < widget.getChildCount(); ++i) {
        View v = widget.getChildAt(i);
        v.setMinimumWidth(pixels);
    }
}

So you don't need to make your own View for TabSpec indicators.

Romulus Urakagi Ts'ai
  • 3,699
  • 10
  • 42
  • 68
  • +1 ! Your solution saved me 30mins of running around between links asking to just add HorizontalScrollView and android:fillViewport="true" ! Thank you! – Madi D. Jun 04 '12 at 12:41
  • A great answer to a question I couldn't find elsewhere. – Guykun Jun 14 '12 at 15:09
4

You can put your TabWidget into a HorizontalScrollView, so you could scroll the tabs.

For an example, please see this post.

If you need buttons on the two side of your tabs, you can wrap further the layout:
put the HorizontalScrollView inside a RelativeLayout, and place Buttons to the left and right side of it. In the buttons' OnClickListener you can scroll the TabWidget programatically.

Community
  • 1
  • 1
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • According to this, putting a TabWidget in a ScrolLView does nothing. http://blog.uncommons.org/2011/04/18/scrolling-tabs-in-android/ – Stealth Rabbi Apr 28 '11 at 12:37
  • Please read carefully the article you've shared, that explains how to proceed. It says it's not enough to put the `TabWidget` into a `ScrollView`, you have to set some attributes as well (as the post i've shared clears it too). – rekaszeru Apr 28 '11 at 12:41
  • Yes, it says to use a HorizontalScrollView, not a ScrollView. Edit : Oh, you said "horizontal ScrollView", and you mean HorizontalScrollView. – Stealth Rabbi Apr 28 '11 at 12:52