Prafull Dhadkar Prafull Dhadkar - 9 months ago 78
Android Question

tabindicator is not sliding from one to another tab in tablayout when I swipe between fragments. Highlighting of font is also not changing

public class LoginRegister extends AppCompatActivity implements SignUpFragment.OnFragmentInteractionListener ,SignInFragment.OnFragmentInteractionListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_register);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout_signup);
tabLayout.addTab(tabLayout.newTab().setText("Sign Up"));
tabLayout.addTab(tabLayout.newTab().setText("Sign in"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager pager =(ViewPager) findViewById(R.id.loginpager);
final SigninPagerAdapter adapter = new SigninPagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
pager.setAdapter(adapter);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});


when I slide through fragments, the highlights of the tab does not changes, But it works fine when I touch the tabs.When I touch them, the tab indicator slides well from left to right and font color of tabs also looks highlighted.
I think Problem is in this piece of code

pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}

@Override
public void onPageSelected(int position) {


}

@Override
public void onPageScrollStateChanged(int state) {

}
});

}


@Override
public void onFragmentInteraction(Uri uri) {

}
}


What should I do to Highlight current tab...

Answer Source

I found my solution.

tabLayout.setupWithViewPager(pager);

this method sets everything easily.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download