How to set selected tab must be in middle
Asked Answered
S

2

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.

Stoke answered 15/12, 2011 at 6:51 Comment(3)
have you looked into using a listener that will detect scrolling and than you manually set the selectedTab in the callback method?Flavine
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); } }Stoke
https://mcmap.net/q/121466/-android-detect-when-scrollview-stops-scrolling/1007273 take a look here, I think you will find helpful ideas.Flavine
A
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);
}
Arnone answered 11/8, 2012 at 20:55 Comment(2)
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 5Panhellenism
After setup , you can call centerTabItem(5) directlyArnone
S
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.

Sexagenarian answered 20/12, 2011 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.