Thillai K Thillai K - 1 year ago 87
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):

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(;

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

tabLayout = (TabLayout) findViewById(;

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


viewPager = (ViewPager) findViewById(;
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event)
return true;
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

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

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

public void onPageScrollStateChanged(int state) {


Answer Source

for this approach you have to implement this method


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) {

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