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.

3
  • have you looked into using a listener that will detect scrolling and than you manually set the selectedTab in the callback method? Commented Dec 15, 2011 at 8: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); } } Commented Dec 15, 2011 at 9:01
  • stackoverflow.com/q/8181828/1007273 take a look here, I think you will find helpful ideas. Commented Dec 15, 2011 at 9:04

2 Answers 2

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); } 
Sign up to request clarification or add additional context in comments.

2 Comments

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
After setup , you can call centerTabItem(5) directly
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.