CallMePedro
CallMePedro

Reputation: 91

How to pretend a click on tabLayout in android kotlin?

is it possible to perform a click on child on tanLayout with code not with "real action"? Something like tab[position].performClick()? Does something like this exists?

Greetings

Upvotes: 1

Views: 303

Answers (3)

Avinash
Avinash

Reputation: 897

if you are using view pager with tab layout then simply use

   mViewPager.setCurrentItem(position)

or if are not using viewpager then use

  mTabLayout.getTabAt(position).select();

Upvotes: 1

Jay Kikani
Jay Kikani

Reputation: 149

You can use something like this.

public static void onClickTab(TabLayout tab_layout) {
        ViewGroup vg = (ViewGroup) tab_layout.getChildAt(0);
        int tabsCount = vg.getChildCount();
        for (int j = 0; j < tabsCount; j++) {
            ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
            int tabChildesCount = vgTab.getChildCount();
            for (int i = 0; i < tabChildesCount; i++) {
                View tabViewChild = vgTab.getChildAt(i);
                if (tabViewChild instanceof TextView) {
                    ((TextView) tabViewChild).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            // Perfor your click acrtion
                        }
                    });
                }
            }
        }
    }

Upvotes: 2

Nehal Godhasara
Nehal Godhasara

Reputation: 795

You can select tab using below code:

 tabs.getTabAt(1)?.select()

Hope it will helps!!

Upvotes: 1

Related Questions