I am having a TabHost that have 5 tabs. I want to center the tab that is selected. So far i tried will give in following.
Declarations in onCreate:
scale = getApplicationContext().getResources().getDisplayMetrics().density;
screenWidth = getWindowManager().getDefaultDisplay().getWidth();
for (int i = 0; i < mTabHost.getTabWidget().getTabCount(); i++)
{
mTabHost.getTabWidget().getChildTabViewAt(i).getLayoutParams().width = (int) tabWidth;
}
This is onTabChanged method:
@Override
public void onTabChanged(String tabId) {
int position = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(position);
mTabHost.getTabWidget().getChildAt(position).setBackgroundResource(R.drawable.tab_background_selector);
SearchIndexActivity searchIndexActivity = new SearchIndexActivity();
Calculation calculation = searchIndexActivity.new Calculation();
calculation.TabCalculation(position);
mHorizontalScrollView.scrollTo(offset, 0);
}
This is the class that makes the calculation for centering the tab.
public class Calculation
{
public void TabCalculation(int position)
{
tabWidth = (int) (150 * scale + 0.5f);
nrOfShownCompleteTabs = ((int) (Math.floor(screenWidth
/ tabWidth) - 1) / 2) * 2;
remainingSpace = (int) ((screenWidth - tabWidth - (tabWidth * nrOfShownCompleteTabs)) / 2);
Log.e("postion in TabCalculation", String.valueOf(position));
//a = (int) (mTabHost.getCurrentTab() * tabWidth);
a = (int) (position * tabWidth);
b = (int) ((int) (nrOfShownCompleteTabs / 2) * tabWidth);
Log.e("a value", String.valueOf(a));
Log.e("b value", String.valueOf(b));
Log.e("tabWidth", String.valueOf(tabWidth));
offset = (a - b) - remainingSpace;
Log.e("offset", String.valueOf(offset));
}
This is all i have done. But tab is not come center. It just come to first tab. Current tab is not coming in center of the screen.
Where am going wrong. Anyone can help me to find out.
.
stackoverflow.comm
No comments:
Post a Comment