Skip to content Skip to sidebar Skip to footer

Dynamically Add And Remove Tabs In Tablayout(material Design) Android

I have a TabLayout and inside that I have ViewPager. I need to dynamically add and remove tab in tablayout(material design). I can able to add the tabs dynamically but while removi

Solution 1:

Remove tab from TabLayout

...
publicvoidremoveTab(int position){
    if (mTabLayout.getTabCount() >= 1 && position<mTabLayout.getTabCount()) {
          mTabLayout.removeTabAt(position);
          mPagerAdapter.removeTabPage(position);
    }
}
...

Add a removeTabPage method to your PagerAdapter

...
public void removeTabPage(int position) {
    if (!tabItems.isEmpty() && position<tabItems.size()) {
          tabItems.remove(position);
          notifyDataSetChanged();
    }
}
...

Add a Tab

...
private void addTab(String title) {
        mTabLayout.addTab(mTabLayout.newTab().setText(title));
        mPagerAdapter.addTabPage(title);
}
...

Add a addTabPage method to your PagerAdapter

...
public void addTabPage(String title) {
      tabItems.add(title);
      notifyDataSetChanged();
}
...

Check out this sample code for a full example: ...samples/SupportDesignDemos/src/com/example/android/support/design/widget/TabLayoutUsage.java

Solution 2:

With the new support library (I'm using 23.2.1) you should only add to the Viewpager and not the TabLayout (otherwise you end up with double tabs and other funky behavior). So to update TouchBoarder's answer:

Add a removeTabPage method to your PagerAdapter

public void removeTabPage(int position) {
    if (!tabItems.isEmpty() && position<tabItems.size()) {
          tabItems.remove(position);
          notifyDataSetChanged();
    }
}

Add a addTabPage method to your PagerAdapter

public void addTabPage(String title) {
      tabItems.add(title);
      notifyDataSetChanged();
}

Solution 3:

Solution 4:

You can add dynamic tabs like this :

 public void addTabs(){
    for(YourClassModel tabName: tabsName){
        Log.i("ActivityClass","tabName"+tabName);
        tabLayout.addTab(tabLayout.newTab().setText(tabName.getName()));

    }

Solution 5:

Removetab from tablayout

TabActivity and ViewPager code :

`public class ScrollableTabsActivity extends AppCompatActivity {

intNumberOfTab=5;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
ViewPagerAdapter adapter ;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scrollable_tabs);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    adapter = newViewPagerAdapter(getSupportFragmentManager());
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

    intlength= tabLayout.getTabCount();

    for (inti=0; i < length; i++) {
        tabLayout.getTabAt(i).setCustomView(getTabView(i));
    }

}

public View getTabView(finalint position) {
    Viewview= LayoutInflater.from(this).inflate(R.layout.close_tablayout, null);
    TextViewtitle= (TextView) view.findViewById(R.id.title);
    ImageViewicon= (ImageView) view.findViewById(R.id.close);
    title.setText(adapter.getPageTitle(position));
    icon.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            NumberOfTab = NumberOfTab - 1;
            setupViewPager(viewPager);
            tabLayout.setupWithViewPager(viewPager);
            if (tabLayout.getTabCount()==3)
                tabLayout.setTabMode(TabLayout.MODE_FIXED);

            intlength= tabLayout.getTabCount();
            for (inti=0; i < length; i++) {
                tabLayout.getTabAt(i).setCustomView(getTabView(i));
            }


        }
    });

    return view;
}

privatevoidsetupViewPager(ViewPager viewPager) {
    ViewPagerAdapteradapter=newViewPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);
}

classViewPagerAdapterextendsFragmentPagerAdapter {

    publicViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Overridepublic Fragment getItem(int position) {
        final Fragment result;
        switch (position) {
            case0:
                // First Fragment of First Tab
                result = newOneFragment();
                break;
            case1:
                // First Fragment of Second Tab
                result = newTwoFragment();
                break;
            case2:
                // First Fragment of Second Tab
                result = newThreeFragment();
                break;
            case3:
                // First Fragment of Second Tab
                result = newFourFragment();
                break;

            case4:
                // First Fragment of Second Tab
                result = newFiveFragment();
                break;

            default:
                result = null;
                break;
        }

        // Log.d("RESULT",result.getTag());return result;
    }

    @OverridepublicintgetCount() {
        return NumberOfTab;
    }

    @Overridepublic CharSequence getPageTitle(finalint position) {
        switch (position) {
            case0:
                return"One";
            case1:
                return"Two";
            case2:
                return"Three";
            case3:
                return"Four";
            case4:
                return"Five";
            default:
                returnnull;
        }
    }

}

}`

Check out this Links : https://stackoverflow.com/a/58480151/9223264

I am new in stackoverflow i try my best if you think my answer is useful then please appreciate

Post a Comment for "Dynamically Add And Remove Tabs In Tablayout(material Design) Android"