How to create tabs dynamically using ViewPager in Android?


Milan Gaella

explain:

Suppose I have multiple categories from REST. I didn't know it had 5 categories, sometimes 2, etc. I want to put all categories on my labels using viewpager. I want to create a tab based on category size. Let's take a look, for example, let's say I only have 2 categories in my REST response, and it only creates 2 tags. If it has 5 categories, it has 5 labels, and so on.

Here is a sample test example to create a tab.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    TabLayout tabLayout;
    ViewPager viewPager;
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

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

        setupViewPager(viewPager);
        viewPager.setCurrentItem(0);
        tabLayout.setupWithViewPager(viewPager);
    }
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        for(int i=0;i<3;i++){
            adapter.addFragment(new OneFragment(), "ONE");
//            adapter.addFragment(new TwoFragment(), "TWO");
            viewPager.setAdapter(adapter);
        }
    }
}

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager){
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment,String title){
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

Please, how can I fix this? ? ?

Drewy

After you get the category size, you can add dynamic labels by category size like this:

public class MainActivity extends AppCompatActivity {

TabLayout tabLayout;
ViewPager viewPager;
Toolbar toolbar;
int no_of_categories=-1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar=(Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

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

    no_of_categories=YOUR_NO_CATEGORIES;

    for (int i = 0; i < no_of_weeks; i++) {
        tabLayout.addTab(tabLayout.newTab().setText("TAB " + String.valueOf(i + 1)));
    }

    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
    viewPager.setAdapter(adapter);

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });


   }
}

and your adapter class should look like this:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    int mNumOfTabs;
    Fragment fragment = null;

    public ViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        for (int i = 0; i < mNumOfTabs ; i++) {
            if (i == position) {
                fragment = YourFragment.newInstance();
                break;
            }
        }
        return fragment;

    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return super.getPageTitle(position);
    }
}

Related


How to create tabs dynamically using ViewPager in Android?

Milan Gaella: illustrate: Suppose I have multiple categories from REST. I didn't know it had 5 categories, sometimes 2, etc. I want to use viewpager to put all taxonomy on my labels. I want to create a tab based on category size. Let's take a look, for example

How to dynamically create tabs in JavaFX using FXML?

username How to create new tags with JavaFX/FXML? I've created a Tabpane in FXML, but I want to click a button that causes a new tab to appear. Here is my FXML: <?import javafx.scene.effect.*?> <?import javafx.scene.text.*?> <?import javafx.scene.layout.*?> <?

How to dynamically create tabs in JavaFX using FXML?

username How to create new tags with JavaFX/FXML? I've created a Tabpane in FXML, but I want to click a button that causes a new tab to appear. Here is my FXML: <?import javafx.scene.effect.*?> <?import javafx.scene.text.*?> <?import javafx.scene.layout.*?> <?

How to dynamically create tabs in JavaFX using FXML?

username How to create new tags with JavaFX/FXML? I've created a Tabpane in FXML, but I want to click a button that causes a new tab to appear. Here is my FXML: <?import javafx.scene.effect.*?> <?import javafx.scene.text.*?> <?import javafx.scene.layout.*?> <?

How to dynamically create tabs in JavaFX using FXML?

username How to create new tags with JavaFX/FXML? I've created a Tabpane in FXML, but I want to click a button that causes a new tab to appear. Here is my FXML: <?import javafx.scene.effect.*?> <?import javafx.scene.text.*?> <?import javafx.scene.layout.*?> <?

How to dynamically create tabs in JavaFX using FXML?

username How to create new tags with JavaFX/FXML? I've created a Tabpane in FXML, but I want to click a button that causes a new tab to appear. Here is my FXML: <?import javafx.scene.effect.*?> <?import javafx.scene.text.*?> <?import javafx.scene.layout.*?> <?

How to dynamically change the color of Viewpager tabs?

Sudheesh Mohan How can I change the color of the label? When I click/swipe to green or any other tab, the tab color should change to that appropriate color and the rest of the other tabs should change to black. How can I do this? I am using Viewpager. I tried

How to dynamically change the color of Viewpager tabs?

Sudheesh Mohan How can I change the color of the label? When I click/swipe to green or any other tab, the tab color should change to that appropriate color and the rest of the other tabs should change to black. How can I do this? I am using Viewpager. I tried

Dynamically hide viewPager tabs

Stephen I want to hide tabs in a specific fragment. I have a MainActivity and two tabs-fragments (I am using viewPager). However, in the third part, I need to hide the tabs and show only the toolbar. I know how to hide the toolbar but nowhere to find how to hi

Dynamically hide viewPager tabs

Stephen I want to hide tabs in a specific fragment. I have a MainActivity and two tabs-fragments (I am using viewPager). However, in the third part, I need to hide the tabs and show only the toolbar. I know how to hide the toolbar but nowhere to find how to hi

Dynamically hide viewPager tabs

Stepan I want to hide tabs in a specific fragment. I have a MainActivity and two tabs-fragments (I am using viewPager). However, in the third fragment, I need to hide the tabs and show only the toolbar. I know how to hide the toolbar but nowhere to find how to

Dynamically hide viewPager tabs

Stephen I want to hide tabs in a specific fragment. I have a MainActivity and two tabs-fragments (I am using viewPager). However, in the third part, I need to hide the tabs and show only the toolbar. I know how to hide the toolbar but nowhere to find how to hi

Dynamically hide viewPager tabs

Stephen I want to hide tabs in a specific fragment. I have a MainActivity and two tabs-fragments (I am using viewPager). However, in the third part, I need to hide the tabs and show only the toolbar. I know how to hide the toolbar but nowhere to find how to hi

How to dynamically create and delete tabs in Foundation 6?

build I want the user to click an e.g. "Add" button in the tabs section to dynamically create the content of the second tab based on the content in the first tab. I achieved this in Bootstrap, but I'm still very stuck on it. Can someone help? Thank you in adva

Android: How to dynamically change the layout of my ViewPager

mr m I have a viewpager that contains pages. Both are basically identical, only one has a different drawable. So all I need to do is change the drawable when the second page is selected and change it back when the first page is selected. I got this all working

Tabs in Toolbar without Viewpager in Android

Noundla Sandeep I went through a lot of answers in SO and didn't have any proper solution to implement tabs in toolbar without Viewpager. I have looked at the SlidingTabsBasic example provided in the sdk examples, however, this does not meet my requirements. P

Nested tabs swiping with ViewPager in Android?

Shaheen Zahedi I have implemented nested tabs using the view pager, which is no problem, but my problem is with the swipe section, When I do a swipe, the main activity tab is switched, but I want the fragment to be swipeable. How can I achieve this? I've tried

Tabs in Toolbar without Viewpager in Android

Noundla Sandeep I went through a lot of answers in SO and didn't have any proper solution to implement tabs in toolbar without Viewpager. I have looked at the SlidingTabsBasic example provided in the sdk examples, however, this does not meet my requirements. P

Tabs in Toolbar without Viewpager in Android

Noundla Sandeep I went through a lot of answers in SO and didn't have any proper solution to implement tabs in toolbar without Viewpager. I have looked at the SlidingTabsBasic example provided in the sdk examples, however, this does not meet my requirements. P

Nested tabs swiping with ViewPager in Android?

Shaheen Zahedi I have implemented nested tabs using the view pager, which is no problem, but my problem is with the swipe section, When I do a swipe, the main activity tab is switched, but I want the fragment to be swipeable. How can I achieve this? I've tried

Nested tabs swiping with ViewPager in Android?

Shaheen Zahedi I have implemented nested tabs using the view pager, which is no problem, but my problem is with the swipe section, When I do a swipe, the main activity tab is switched, but I want the fragment to be swipeable. How can I achieve this? I've tried

Tabs in Toolbar without Viewpager in Android

Noundla Sandeep I went through a lot of answers in SO and didn't have any proper solution to implement tabs in toolbar without Viewpager. I have looked at the SlidingTabsBasic example provided in the sdk examples, however, this does not meet my requirements. P

Tabs in Toolbar without Viewpager in Android

Noundla Sandeep I went through a lot of answers in SO and didn't have any proper solution to implement tabs in toolbar without Viewpager. I have looked at the SlidingTabsBasic example provided in the sdk examples, however, this does not meet my requirements. P

Nested tabs swiping with ViewPager in Android?

Shaheen Zahedi I have implemented nested tabs using the view pager, which is no problem, but my problem is with the swipe section, When I do a swipe, the main activity tab is switched, but I want the fragment to be swipeable. How can I achieve this? I've tried

Nested tabs swiping with ViewPager in Android?

Shaheen Zahedi I have implemented nested tabs using the view pager, which is no problem, but my problem is with the swipe section, When I do a swipe, the main activity tab is switched, but I want the fragment to be swipeable. How can I achieve this? I've tried

Dynamically create tabs from lists using Bootstrap and Thymeleaf

Andrea T Given that I have a list of objects "Bar", each Bar has two properties "title" and "content": How can I dynamically create a tab with a palette for each Bar in the list? This html is invalid: the opened tabs are clicked one by one, as shown in the fol