Thillai K Thillai K - 1 month ago 12
Android Question

Android: how to highlight the title(as if it is selected) of the next tab in tab layout after navigation

I have created a sign-up form with tab layout which includes three tabs(three fragments). I have successfully ended up in navigating to the next fragment after clicking on 'Next' in my previous fragment. However my problem is that I am unable to highlight my current tab, which should be highlighted(as if it is selected) when I click 'Next' in my previous fragment.

The following is my first fragment(Account info):

@Override
public void onClick(View v) {
emailid = email.getText().toString();
if (!isValidEmail(emailid)) {
email.setError("Invalid Email");
}

pass = password.getText().toString();
if (!isValidPassword(pass)) {
password.setError("Password cannot be empty");
}
confirmPass = confirmPassword.getText().toString();
if (!isValidPassword(confirmPass)) {
confirmPassword.setError("Password cannot be empty");
}


I am navigating to the next fragment using the below code, could anyone suggest how to highlight the title of the next tab(fragment) after navigating to it?

if (isValidEmail(emailid) && isValidPassword(pass)) {
viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
viewPager.setCurrentItem(1);
}
}


below is my activity code, how do i disable touch or click events on tabs?

tabLayout = (TabLayout) findViewById(R.id.tabLayout);

tabLayout.addTab(tabLayout.newTab().setText("Account Info"));
tabLayout.addTab(tabLayout.newTab().setText("Personal Info"));
tabLayout.addTab(tabLayout.newTab().setText("Confirmation"));

tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

viewPager = (ViewPager) findViewById(R.id.pager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
viewPager.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event)
{
return true;
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}
// This method will be invoked when a new page becomes selected.
@Override
public void onPageSelected(int position) {
Toast.makeText(SignupActivity.this, "Selected page position: " + position, Toast.LENGTH_SHORT).show();
tabLayout.tabP(true);
}

@Override
public void onPageScrollStateChanged(int state) {

}
});

Answer

for this approach you have to implement this method

viewPager.addOnPageChangeListener

and in onPageSelected method you have to re-write the tab name as per condition

you can implement a method like this

 private void initTabsName() {
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            RelativeLayout relativeLayout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.subcat_custom_tab, tabLayout, false);
      // do something to make tab selected using if else statements
        /* // for example
           if (i == 0) {

            }
         */
        tab.setCustomView(relativeLayout);
        }
    }
Comments