Vinothkumar Nataraj
Vinothkumar Nataraj

Reputation: 588

How to get active tab position in android material

How to get active tab position in material tabs?
Any one help me.

Upvotes: 1

Views: 3255

Answers (4)

Vishal Patoliya ツ
Vishal Patoliya ツ

Reputation: 3238

Try this

 tabLayout.setOnTabSelectedListener(
                    new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
                        @Override
                        public void onTabSelected(TabLayout.Tab tab) {
                            super.onTabSelected(tab);
                            position = tab.getPosition();

                        }
                    });
        }

Upvotes: 1

Onkar Nene
Onkar Nene

Reputation: 1379

In your Activity implement OnTabChangeListener, then set the listener for the TabHost tabHost.setOnTabChangedListener(this);

@Override
    public void onTabChanged(String id) {
        Log.i("INDEX->", "Selected TAB Index - "+ tabHost.getCurrentTab());
}

Upvotes: 1

Sivakumar
Sivakumar

Reputation: 347

Try this

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

Upvotes: 2

XxGoliathusxX
XxGoliathusxX

Reputation: 972

   tabLayout.setOnTabSelectedListener(
                    new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
                        @Override
                        public void onTabSelected(TabLayout.Tab tab) {
                            super.onTabSelected(tab);
                            currentPage = tab.getPosition();

                        }
                    });
        }

Upvotes: 3

Related Questions