7

I am new to Android and in my project I have requirement

i.e I have six tabs but I show only three tabs on screen and for rest of tabs I use ScrollView.Here selected tab item must be in the middle,first when I enter into screen I show middle item is active(i.e selected).

That's fine but how could I set this middle item as middle when I scroll for remaining Tabs?

can anybody give suggestion for getting solution.

Vivek Kalkur
  • 2,200
  • 2
  • 21
  • 40
Gopi
  • 171
  • 3
  • 8
  • have you looked into using a listener that will detect scrolling and than you manually set the selectedTab in the callback method? – hovanessyan Dec 15 '11 at 08:53
  • Thanks for your suggestion but i didn't work the following scrolling listeners (i declared Horizaontalscrollview in xml) TestHorizontalScrollView sView = (TestHorizontalScrollView)findViewById(R.id.horizontalScrollView);public class TestHorizontalScrollView extends HorizontalScrollView { public TestHorizontalScrollView(Context context) { super(context); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); } } – Gopi Dec 15 '11 at 09:01
  • http://stackoverflow.com/q/8181828/1007273 take a look here, I think you will find helpful ideas. – hovanessyan Dec 15 '11 at 09:04

2 Answers2

7

Check this out :)

public void centerTabItem(int position) {
    tabHost.setCurrentTab(position);
    final TabWidget tabWidget = tabHost.getTabWidget();
    final int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
    final int leftX = tabWidget.getChildAt(position).getLeft();
    int newX = 0;

    newX = leftX + (tabWidget.getChildAt(position).getWidth() / 2) - (screenWidth / 2);
    if (newX < 0) {
        newX = 0;
    }
    horizontalScrollView.scrollTo(newX, 0);
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Alok Kulkarni
  • 2,153
  • 19
  • 31
  • this works very well. but how do i get this to happen for the first time when the tabhost is setup and initialized. lets say i want to initailioze the tab host with the current index as 5 – Sunny Dec 10 '13 at 11:05
  • After setup , you can call centerTabItem(5) directly – Alok Kulkarni Mar 21 '14 at 07:20
0

I don't think the tabhost have such behavior. As I know, tabhost can work without tab controller. For your case, a gallery will replace the tab controller, as you know, the selected item in gallery always stay in the middle. Then in gallery's event listener, write some code to control which tab will show in tabhost.

David Guo
  • 1,749
  • 3
  • 20
  • 30