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...
ViewPager is used to swipe between the tabs. WhatsApp, Facebook, etc are a very good example of TabLayout with ViewPager. This is how a TabLayout looks like. select new -> fragment -> select Fragment (Blank). Follow above step for CourseFragment and LoginFragment. Now add the following code in AlgorithmFragment.xml file.
the background color overlap the tab indicator. that's why the tab indicator is not visible. If you remove tabBackground color property then you can see the indicator. As I said it's bug on Design Support library.
If we need to set the Indicator color, without any custom Indicator, so in that case, we only need to set the app:tabIndicatorColor as you can from the below XML file: From the above XML, you will see the normal TabLayout Indicator color, without any custom view.
For bottom padding, we need to add android:paddingBottom to TabLayout, Like the below code snippet, and we will see the result. For Left/Right Indicator Margins, we need to add margin in our drawable file <item> tag, which we created earlier. In the above code, what I did is to add an end and `margin to the <item> tag, and we will see the result.
I found my solution.
tabLayout.setupWithViewPager(pager);
this method sets everything easily.
Just an additional note.
Best thing to do is call tabLayout.setupWithViewPager(viewPager)
last.
At least, don't call viewPager.clearOnPageChangeListeners()
or viewPager.setOnPageChangeListener()
right after you called tabLayout.setupWithViewPager(viewPager)
because that will remove the listener that TabLayout uses when the ViewPager
is scrolled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With