Gopi
Gopi

Reputation: 171

How to set selected tab must be in middle

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.

Upvotes: 7

Views: 2941

Answers (2)

Alok Kulkarni
Alok Kulkarni

Reputation: 2153

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);
}

Upvotes: 7

David Guo
David Guo

Reputation: 1759

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.

Upvotes: 0

Related Questions