Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 974
Prafull Dhadkar Avatar asked Mar 15 '16 17:03

Prafull Dhadkar


People also ask

How to swipe between tabs in Android apps using tablayout?

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.

Why can't I see the tab indicator?

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.

How to set the indicator color of the tablayout indicator?

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.

How to add bottom padding in Android tablayout?

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.


2 Answers

I found my solution.

tabLayout.setupWithViewPager(pager);

this method sets everything easily.

like image 78
Prafull Dhadkar Avatar answered Oct 27 '22 20:10

Prafull Dhadkar


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.

like image 40
Markymark Avatar answered Oct 27 '22 19:10

Markymark