Code example for TabWidget

Methods: getChildAt, getParent

0
 
        tabHost.setCurrentTab(position);
        postDelayed(new Runnable() {
            @Override 
            public void run() { 
                View tabView = widget.getChildAt(position);
                if (tabView != null) {
                    HorizontalScrollView scrollView = (HorizontalScrollView) widget.getParent();
                    assert scrollView != null;
                    scrollView.setFillViewport(true);
 
                    final int scrollPos = tabView.getLeft() - (scrollView.getWidth() - tabView.getWidth()) / 2;
                    scrollView.smoothScrollTo(scrollPos, 0);
                    scrollView.postDelayed(new Runnable() {
                        @Override 
                        public void run() { 
                            onTabPageSelected(position);
                        } 
                    }, 250); 
                } 
            }