Search code examples
androidandroid-tabsandroid-tablayout

How to "RE-SELECT" tab in TabLayout programmatically?


TabLayout.OnTabSelectedListener has one callback which is onTabReselected(TabLayout.Tab tab)

We can reselect tab manually by tapping on selected tab again.

So my question is how to reselect tab programatically?


Solution

  • This is how I got my tabs working

    Layout for tabs:

     <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="#ffffff">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"
                app:layout_collapseMode="pin"
                android:background="#000000"
                app:titleTextColor="#ffffff"
    
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/PopupMenuStyle">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:id="@+id/textview"
                    android:textColor="@color/colorTrueWhite"/>
    
            </android.support.v7.widget.Toolbar>
    
            <!-- our tablayout to display tabs  -->
            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorBlack"
                android:minHeight="?attr/actionBarSize"
    
                app:tabIndicatorColor="@color/colorTrueWhite"
                app:tabIndicatorHeight="5dp"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
    
            <!-- View pager to swipe views -->
    
    
            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"/>
    
    
        </LinearLayout>
    

    Activity using that layout:

    public class Main2Activity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
    
        private TabLayout tabLayout;
        public static ViewPager viewPager;
        public static Context ctx;
        Pager adapter;
        public static int expired, paid;
        Boolean isConnection;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
    
            Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(tb);
    
            TextView tv = (TextView) findViewById(R.id.textview);
    
            //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    
            ctx = getApplicationContext();
    
    
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
    
    
            tabLayout = (TabLayout) findViewById(R.id.tabLayout);
            viewPager = (ViewPager) findViewById(R.id.pager);
    
    
            tabLayout.addTab(tabLayout.newTab().setText("title1"));
            tabLayout.addTab(tabLayout.newTab().setText("title2"));
            tabLayout.addTab(tabLayout.newTab().setText("title3"));
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
            tabLayout.setupWithViewPager(viewPager);
    
            adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount(), ctx);
            viewPager.setAdapter(adapter);
    
    
            if(some condition)
            {
                viewPager.setCurrentItem(2);
            }
            else
            {
                viewPager.setCurrentItem(1);
            }
            viewPager.setOffscreenPageLimit(2);
    
            tabLayout.addOnTabSelectedListener(this);
    
            ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
            int tabsCount = vg.getChildCount();
            for (int j = 0; j < tabsCount; j++) {
                ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
                int tabChildsCount = vgTab.getChildCount();
                for (int i = 0; i < tabChildsCount; i++) {
                    View tabViewChild = vgTab.getChildAt(i);
                    if (tabViewChild instanceof TextView) {
                        ((TextView) tabViewChild).setTypeface(Typeface.DEFAULT, Typeface.BOLD);
                    }
                }
            }
        }
    
    
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }
    
        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }
    
    
    }
    

    Pager code:

    public class Pager extends FragmentStatePagerAdapter
    {
        int tabcount;
        Context ctx;
        private String [] Titles = {"title1", "title2", "title3"};
    
        public Pager(FragmentManager fm, int tabcount, Context ctx)
        {
            super(fm);
            this.tabcount = tabcount;
            this.ctx = ctx;
        }
    
        @Override
        public int getCount() {
            return tabcount;
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            return Titles[position];
        }
    
        @Override
        public Fragment getItem(int position) {
    
            switch (position) {
    
                case 0:
                    Tab1 tab1 = new Tab1();
                    return tab1;
    
                case 1:
                    Tab2 tab2 = new Tab2();
                    return tab2;
    
                case 2:
                    Tab3 tab3 = new Tab3();
                    return tab3;
    
                default:
                    return null;
            }
        }
    
    
    }