Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 579
Gopi Avatar asked Dec 15 '11 06:12

Gopi


1 Answers

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);
}
like image 63
Alok Kulkarni Avatar answered Sep 20 '22 23:09

Alok Kulkarni