title: Android How to add Tabs dynamically from json using viewpager
body: <p>How to add Tabs dynamically from json using viewpager at load page on Mainactivity and show corresponding data on gridview.
  I already try this ,just static method.</p>
  
  <p>can i add dynamically tabs from json? and it's data corresponding.</p>
  
  <pre><code> ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
      if (viewPager != null) {
          setupViewPager(viewPager);
      }
      TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
      tabLayout.setupWithViewPager(viewPager);
  
      ViewPager viewPager1 = (ViewPager) findViewById(R.id.viewpager1);
      if (viewPager1 != null) {
          setupViewPager1(viewPager1);
      }
      TabLayout tabLayout1 = (TabLayout) findViewById(R.id.tabs1);
      tabLayout1.setupWithViewPager(viewPager1);
  
  }
  
  public void setTabFragmentTwo(String t){
      TabFragmentTwo = t;
  }
  
  public String getTabFragmentTwo(){
      return TabFragmentTwo;
  }
  
  
  private void setupViewPager(ViewPager viewPager) {
      Adapter adapter = new Adapter(getSupportFragmentManager());
      adapter.addFragment(new OneFragment(), "Category 1");
      adapter.addFragment(new TwoFragment(), "Category 2");
      adapter.addFragment(new ThreeFragment(), "Category 3");
      adapter.addFragment(new ThreeFragment(), "Category 3");
      adapter.addFragment(new ThreeFragment(), "Category 3");
      adapter.addFragment(new ThreeFragment(), "Category 3");
      viewPager.setAdapter(adapter);
  }
  static class Adapter extends FragmentPagerAdapter {
      private final List&lt;Fragment&gt; mFragments = new ArrayList&lt;&gt;();
      private final List&lt;String&gt; mFragmentTitles = new ArrayList&lt;&gt;();
  
      public Adapter(FragmentManager fm) {
          super(fm);
      }
  
      public void addFragment(Fragment fragment, String title) {
          mFragments.add(fragment);
          mFragmentTitles.add(title);
      }
  
      @Override
      public Fragment getItem(int position) {
          return mFragments.get(position);
      }
  
      @Override
      public int getCount() {
          return mFragments.size();
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return mFragmentTitles.get(position);
      }
  }
  private void setupViewPager1(ViewPager viewPager) {
      Adapter1 adapter1 = new Adapter1(getSupportFragmentManager());
      adapter1.addFragment(new Fragment_quick_bill(), "Quick Bill");
      adapter1.addFragment(new Fragment_addvnced_bill(), "Addvanc-Bil");
  
      viewPager.setAdapter(adapter1);
  }
  static class Adapter1 extends FragmentPagerAdapter {
      private final List&lt;Fragment&gt; mFragments = new ArrayList&lt;&gt;();
      private final List&lt;String&gt; mFragmentTitles = new ArrayList&lt;&gt;();
  
      public Adapter1(FragmentManager fm) {
          super(fm);
      }
  
      public void addFragment(Fragment fragment, String title) {
          mFragments.add(fragment);
          mFragmentTitles.add(title);
      }
  
      @Override
      public Fragment getItem(int position) {
          return mFragments.get(position);
      }
  
      @Override
      public int getCount() {
          return mFragments.size();
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return mFragmentTitles.get(position);
      }
  }
  </code></pre>