title: How can i create dynamically tab with viewpager in android?
body: <p><strong>Explanation:</strong></p>
  
  <p>Suppose, I have multiple categories which come from my REST. I don't know how many categories it's mat be 5,7 or sometimes 2 etc. I want to put all the categories on my tab with viewpager. I want to create a tab accordingly to the categories size. let's see e.g. suppose in my REST response have only 2 categories it's create only two tab. if it have 5 categories then 5 tab and so on.</p>
  
  <p>Here is the sample test example to create a tabs.</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>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&lt;3;i++){
              adapter.addFragment(new OneFragment(), "ONE");
  //            adapter.addFragment(new TwoFragment(), "TWO");
              viewPager.setAdapter(adapter);
          }
      }
  }
  </code></pre>
  
  <p><strong>ViewPagerAdapter.java</strong></p>
  
  <pre><code>public class ViewPagerAdapter extends FragmentPagerAdapter {
  
      private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
      private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
      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);
      }
  }
  </code></pre>
  
  <p>Please, How can I solve out???</p>