title: How can I create dynamic tab with viewpager(android)?
body: <p>I Want to create dynamically tabs with viewpager,but I don't know How to do this.
  I will get an jsonstring from my webservice API like this:</p>
  
  <pre><code>{"status":0,"data":[{"category":"fruit","pic":"URL","Content":"apple"},{"category":"fruit","pic":"URL","Content":"banana"},{"category":"fruit","pic":"URL","Content":"papaya"},{"category":"drink","pic":"URL","Content":"milktea"},{"category":"drink","pic":"URL","Content":"blacktea"},{"category":"drink","pic":"URL","Content":"greentea"},{"category":"animal","pic":"URL","Content":"dog"},{"category":"animal","pic":"URL","Content":"cat"},{"category":"animal","pic":"URL","Content":"lion"}]}
  </code></pre>
  
  <p>I want to create the tabs form category(fruit,drink,animal)with viewpager,and viewpager set fragment(fragment just has listview,listview has a picture and text)</p>
  
  <p>Now my code is static</p>
  
  <p>I just set three tabs and three fragments to do this.</p>
  
  <p>the screen:</p>
  
  <p><a href="https://i.stack.imgur.com/r0jpF.jpg" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>and my code now:</p>
  
  <p>Food.java</p>
  
  <pre><code>public class Food extends AppCompatActivity {
  Toolbar toolbar;
  TabLayout tabLayout;
  ViewPager viewPager;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_food);
      toolbar = (Toolbar) findViewById(R.id.toolbar);
      toolbar.setTitle("");
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      getSupportActionBar().setDisplayShowHomeEnabled(true);
      viewPager = (ViewPager) findViewById(R.id.viewpager);
      viewPager.setOffscreenPageLimit(3);
      setupViewPager(viewPager);
      tabLayout = (TabLayout) findViewById(R.id.tabs);
      tabLayout.setupWithViewPager(viewPager);
  }
  
  private void setupViewPager(ViewPager viewPager) {
      FoodViewPagerAdapter adapter = new FoodViewPagerAdapter(getSupportFragmentManager());
      adapter.addFragment(new Fragment_Food_one(), "FRUIT");
      adapter.addFragment(new Fragment_Food_two(), "DRINK");
      adapter.addFragment(new Fragment_Food_three(), "ANIMAL");
      viewPager.setAdapter(adapter);
  }
  </code></pre>
  
  <p>activity_food.xml</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="edu.shu.foodtest.Food"&gt;
  &lt;android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/ThemeOverlay.AppCompat.Light"&gt;
  
      &lt;android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="40dp"
          android:background="#EEEEEE"
          android:theme="@style/ThemeOverlay.AppCompat.Light"
          app:popupTheme="@style/AppTheme.PopupOverlay"&gt;
          &lt;TextView
              android:id="@+id/toolbar_title"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:singleLine="true"
              android:text="test"
              android:textColor="@color/red"
              android:textSize="18sp"
              android:textStyle="bold" /&gt;
      &lt;/android.support.v7.widget.Toolbar&gt;
  
      &lt;android.support.design.widget.TabLayout
          android:id="@+id/tabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:tabMode="fixed"
          app:tabGravity="fill"
          android:background="#FFFFFF"/&gt;
  &lt;/android.support.design.widget.AppBarLayout&gt;
  
  &lt;android.support.v4.view.ViewPager
      android:id="@+id/viewpager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#EEEEEE"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>FoodViewPagerAdapter.java:</p>
  
  <pre><code>public class FoodViewPagerAdapter extends FragmentPagerAdapter {
  private final List&lt;android.support.v4.app.Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
  private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
  public FoodViewPagerAdapter(FragmentManager manager) {
      super(manager);
  }
  
  @Override
  public android.support.v4.app.Fragment getItem(int position) {
      return mFragmentList.get(position);
  }
  
  @Override
  public int getCount() {
      return mFragmentList.size();
  }
  
  public void addFragment(android.support.v4.app.Fragment fragment, String title) {
      mFragmentList.add(fragment);
      mFragmentTitleList.add(title);
  }
  
  @Override
  public CharSequence getPageTitle(int position) {
      return mFragmentTitleList.get(position);
  }
  </code></pre>
  
  <p>Fragment_Food_one.java:</p>
  
  <pre><code>public class Fragment_Food_one extends Fragment {
  View view;
  ListView mListView;
  TextView BrandID;
  String brandid;
  ArrayList&lt;Foodlistclass&gt; arraylist = new ArrayList&lt;Foodlistclass&gt;();
  private String result;
  JSONObject response_object;
  Context context;
  String jsonstring="{\"status\":0,\"data\":[{\"category\":\"fruit\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"apple\"},{\"category\":\"fruit\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"banana\"},{\"category\":\"fruit\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"papaya\"},{\"category\":\"drink\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"milktea\"},{\"category\":\"drink\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"blacktea\"},{\"category\":\"drink\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"greentea\"},{\"category\":\"animal\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"dog\"},{\"category\":\"animal\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"cat\"},{\"category\":\"animal\",\"pic\":\"http://XXX.XXX.XXX.XXX/XXX.jpg\",\"Content\":\"lion\"}]}";
  
  public Fragment_Food_one() {
      // Required empty public constructor
  }
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      view = inflater.inflate(R.layout.fragment_food_one, container, false);
  
      context = getActivity().getApplicationContext();
      mListView = (ListView) view.findViewById(R.id.listviewitem);
      try {
          setlistview();
      } catch (JSONException e) {
          e.printStackTrace();
      }
      return view;
  }
  
  public void setlistview() throws JSONException {
      jsonstring = jsonstring.replace("\\", "");
      response_object = new JSONObject(jsonstring.substring(jsonstring.indexOf("{"), jsonstring.lastIndexOf("}") + 1));
      String status = response_object.getString("status");
      if (status.equals("0")) {
          JSONArray responseData = response_object.optJSONArray("data");
          for (int i = 0; i &lt; responseData.length(); i++) {
              Map&lt;String, Object&gt; item = new HashMap&lt;String, Object&gt;();
              JSONObject jsonChildNode = responseData.getJSONObject(i);
              String category = jsonChildNode.optString("category");
              System.out.println(category);
              String Content = jsonChildNode.optString("Content");
              System.out.println(Content);
              String pic = jsonChildNode.optString("pic");
              System.out.println(pic);
              if (category.equals("fruit")) {
                  arraylist.add(new Foodlistclass(
                          pic, Content, "a", "aW"
                  ));
              }
              FoodlistAdapter adapter = new FoodlistAdapter(context, R.layout.food_listitem, arraylist);
              mListView.setAdapter(adapter);
          }
      }
  }
  </code></pre>
  
  <p>fragment_food_one.xml:</p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  
  &lt;!-- TODO: Update blank fragment layout --&gt;
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@android:color/transparent"
      android:orientation="vertical"&gt;
  
      &lt;ListView
          android:id="@+id/listviewitem"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@android:color/transparent"
          android:divider="@android:color/transparent"
          android:dividerHeight="10dp"
          android:scrollbars="none"&gt;&lt;/ListView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>I want to change it to dynamically because it will add more category in the future,If I use dynamically tabs it will add tabs automatically from jsonstring</p>
  
  <p>I'm the newer for android developer,so many things I don't know.I research many data but I can't achieve what I want to do.</p>
  
  <p>Hope someone can tell me How to do or give me a sample to do this.</p>
  
  <p>Thanks all and I want to say sorry for my poor English,I hope you can understand my meaning above.</p>
  
  <p>Thanks and Thanks!!!</p>