title: Tab layout with Fragments
body: <p>I am trying to load different fragments on each tab with Android Tablayout and PagerAdapter. Tabs are working fine however corresponding fragment is not loading on my scree. Here is the code:</p>
  
  <pre><code>import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  
  public class Tab1 extends Fragment {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          return inflater.inflate(R.layout.tab1, container, false);
      }
  }
  </code></pre>
  
  <p>Created total 3 tab classes for each fragment XML. Here is my xml for Fragment:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerInParent="true"
          android:text="Tab 1"
          android:textAppearance="?android:attr/textAppearanceLarge"/&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>My Activity class:</p>
  
  <pre><code>import android.app.ActionBar;
  import android.content.Intent;
  import android.support.design.widget.TabLayout;
  import android.support.v4.view.ViewPager;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.support.v7.widget.Toolbar;
  import android.widget.Toast;
  
  import java.util.logging.Logger;
  
  public class DashboardActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{
  
      //This is our tablayout
      private TabLayout tabLayout;
  
      //This is our viewPager
      private ViewPager viewPager;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_dashboard);
  
          //Adding toolbar to the activity
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          //Initializing the tablayout
          tabLayout = (TabLayout) findViewById(R.id.tabLayout);
  
          //Adding the tabs using addTab() method
          tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title"));
          tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title"));
          tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title"));
          tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
  
          //Initializing viewPager
          viewPager = (ViewPager) findViewById(R.id.pager);
  
          //Creating our pager adapter
          Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());
  
          //Adding adapter to pager
          viewPager.setAdapter(adapter);
  
          //Adding onTabSelectedListener to swipe views
          tabLayout.setOnTabSelectedListener(this);
      }
  
      @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) {
  
      }
  }
  </code></pre>
  
  <p>My pager adapter class is :</p>
  
  <pre><code>public class Pager extends FragmentStatePagerAdapter {
  
      //integer to count number of tabs
      int tabCount;
  
      //Constructor to the class
      public Pager(FragmentManager fm, int tabCount) {
          super(fm);
          //Initializing tab count
          this.tabCount= tabCount;
      }
  
      //Overriding method getItem
      @Override
      public Fragment getItem(int position) {
          //Returning the current tabs
          switch (position) {
              case 0:
                  Tab1 tab1 = new Tab1();
                  return tab1;
              case 1:
                  Tab2 tab2 = new Tab2();
                  return tab2;
              case 2:
                  Tab3 tab3 = new Tab3();
                  return tab3;
              default:
                  return null;
          }
      }
  
      //Overriden method getCount to get the number of tabs
      @Override
      public int getCount() {
          return tabCount;
      }
  }
  </code></pre>
  
  <p>Activity Dashboard Layout file:</p>
  
  <p>
  </p>
  
  <pre><code>&lt;android.support.design.widget.TabLayout
      android:id="@+id/tabLayout"
      android:layout_width="368dp"
      android:layout_height="wrap_content"
      app:tabMode="fixed"
      tools:layout_editor_absoluteX="8dp"
      app:layout_constraintTop_toTopOf="parent"
      android:layout_marginTop="8dp"
      android:onClick="loadOption"/&gt;
  
  &lt;android.support.v4.view.ViewPager
      android:id="@+id/pager"
      android:layout_width="0dp"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:background="@android:color/white"
      tools:layout_editor_absoluteY="0dp"
      tools:layout_editor_absoluteX="8dp"
      tools:ignore="MissingConstraints" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>Styles.xml file:</p>
  
  <pre><code>&lt;resources&gt;
  
      &lt;!-- Base application theme. --&gt;
      &lt;style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"&gt;
          &lt;!-- Customize your theme here. --&gt;
          &lt;item name="colorPrimary"&gt;@color/colorPrimary&lt;/item&gt;
          &lt;item name="colorPrimaryDark"&gt;@color/colorPrimaryDark&lt;/item&gt;
          &lt;item name="colorAccent"&gt;@color/colorAccent&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;style name="AppTheme.NoActionBar"&gt;
          &lt;item name="windowActionBar"&gt;false&lt;/item&gt;
          &lt;item name="windowNoTitle"&gt;true&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /&gt;
  
      &lt;style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /&gt;
  
  &lt;/resources&gt;
  </code></pre>
  
  <p>Any suggestions. </p>