title: I want to show two fragments when I click a tab
body: <p><img src="https://i.stack.imgur.com/yJjD1.png" alt="It will be like this" /></p>
  <p>How to display two fragments at the same time using TabLayout?</p>
  <p>// main.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:orientation=&quot;vertical&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
  
     &lt;androidx.viewpager.widget.ViewPager
         android:id=&quot;@+id/pager&quot;
         android:layout_width=&quot;match_parent&quot;
         android:layout_height=&quot;200dp&quot;&gt;
  
     &lt;/androidx.viewpager.widget.ViewPager&gt;
     &lt;androidx.viewpager.widget.ViewPager
         android:layout_width=&quot;match_parent&quot;
         android:layout_height=&quot;match_parent&quot;
         android:id=&quot;@+id/viewPager&quot;&gt;
        
        &lt;com.google.android.material.tabs.TabLayout
            android:id=&quot;@+id/tab_layout&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            app:tabMode=&quot;scrollable&quot;/&gt;
        
     &lt;/androidx.viewpager.widget.ViewPager&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  <p>// main.java</p>
  <pre><code>public class MainActivity extends AppCompatActivity {
      //initialize variable
      TabLayout tabLayout;
      ViewPager viewPager,pager;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          //assign variable
          tabLayout=findViewById(R.id.tab);
          viewPager=findViewById(R.id.viewPager);
          pager=findViewById(R.id.pager);
  
          viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
          pager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
          tabLayout.setupWithViewPager(viewPager);
          tabLayout.setupWithViewPager(pager);
          tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
              @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>// pagerAdapter.java</p>
  <pre><code>public class PagerAdapter extends FragmentPagerAdapter {
      String[] text={&quot;AAAA&quot;,&quot;BBBB&quot;,&quot;CCCC&quot;};
      @SuppressLint(&quot;WrongConstant&quot;)
      public PagerAdapter(@NonNull FragmentManager fm) {
          super(fm, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
      }
      @NonNull
      @Override
      public Fragment getItem(int position) {
          if(position==0){
              return new  AFragment();
          }
          if(position==1){
              return new  Bfragment();
          }
          if(position==2){
              return new  CFragment();
          }
          return null;
      }
      @Override
      public int getCount() {
          return text.length;
      }
      @Nullable
      @Override
      public CharSequence getPageTitle(int position) {
          return text[position];
      } }
  </code></pre>
  <p>If you know any idea, can you help me?
  <a href="https://stackoverflow.com/questions/48354457/how-to-display-two-fragments-at-the-same-time-using-tablayout">How to display two fragments at the same time using TabLayout?</a>
  a similar problem, but I don't know how to apply the solution to my task</p>