title: Why is my tab layout for android not working?
body: <p>I am trying to make a tab sliding activity for my app but I am not able to find what is going wrong.</p>
  
  <p>Here is the code for the tab activity:</p>
  
  <pre><code>public class TabActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_tab);
  
      Toolbar toolbar = findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      ViewPager viewPager = findViewById(R.id.container);
      viewPager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager()));
      TabLayout tabLayout = findViewById(R.id.tabs);
      viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
      tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_tab, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();
  
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  public static abstract class LayoutFragment extends Fragment {
      private final int layout;
  
      public LayoutFragment(@LayoutRes int layout) {
          this.layout = layout;
      }
  
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          return inflater.inflate(layout, container, false);
      }
  
      @RequiresApi(api = Build.VERSION_CODES.KITKAT)
      @Override
      public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
      }
  }
  
  public static class Fragment1 extends LayoutFragment {
      public Fragment1() {
          super(R.layout.fragment_one);
      }
  }
  
  public static class Fragment2 extends Fragment {
      public Fragment2() {
          super(R.layout.fragment_two);
      }
  }
  
  public class SectionsPagerAdapter extends FragmentPagerAdapter {
  
      SectionsPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
          // getItem is called to instantiate the fragment for the given page.
          switch (position) {
              case 0:
                  return new Fragment1();
              default:
                  return new Fragment2();
          }
      }
  
      @Override
      public int getCount() {
          return 2;
      }
  }
  }
  </code></pre>
  
  <p>The code is compiling perfectly but the tabs for fragment1 and fragment2 are not appearing at all.</p>
  
  <p>I am not able to figure out what's going wrong and where, I would appreciate any help.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Here is my XML code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.coordinatorlayout.widget.CoordinatorLayout   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:id="@+id/main_content"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/wallpaper"
  android:fitsSystemWindows="true"
  tools:context=".TabActivity"&gt;
  
  &lt;com.google.android.material.appbar.AppBarLayout
      android:id="@+id/appbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingTop="@dimen/appbar_padding_top"
      android:theme="@style/AppTheme.AppBarOverlay"&gt;
  
      &lt;androidx.appcompat.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:layout_weight="1"
          android:background="?attr/colorPrimary"
          app:layout_scrollFlags="scroll|enterAlways"
          app:popupTheme="@style/AppTheme.PopupOverlay"
          app:title="@string/app_name"&gt;
  
      &lt;/androidx.appcompat.widget.Toolbar&gt;
  
      &lt;com.google.android.material.tabs.TabLayout
          android:id="@+id/tabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"&gt;
  
          &lt;com.google.android.material.tabs.TabItem
              android:id="@+id/tabItem"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/tab1" /&gt;
  
          &lt;com.google.android.material.tabs.TabItem
              android:id="@+id/tabItem2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/tab2" /&gt;
  
      &lt;/com.google.android.material.tabs.TabLayout&gt;
  &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  
  &lt;androidx.viewpager.widget.ViewPager
      android:id="@+id/container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /&gt;
  </code></pre>
  
  <p></p>