title: Android : How to make a tab not selectable/swipable as per radiobutton click
body: <p>In my android activity I have a <code>radio button group</code> and a <code>tab layout</code> using <code>viewpager</code>.
  There are two tabs and 2 radio buttons.</p>
  <ul>
  <li>The first tab is an attachment tab</li>
  <li>second one is an event tab.</li>
  </ul>
  <p>When i checked the first radio button then user is not allowed to see the first tab,so i need to disable the first tab and shows the second tab for the user.If i selected the second radio button i need to enable the first tab and user is allowed to access the first tab.I don't want to remove the tab,i just want to disable it so that user can't access it by swiping or clicking on the tab.</p>
  <h1>How to do that? Below is my code to show tab.</h1>
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.create_asset);
      //remaining code
      viewPager2 = (ViewPager) findViewById(R.id.viewPager2);
      setupViewPager2(viewPager2);
      tabLayout2 = (TabLayout) findViewById(R.id.tab_layout2);
      tabLayout2.setupWithViewPager(viewPager2);//setting tab over viewpager
      rdbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      }
      });
  
          @Override
          public void onCheckedChanged(RadioGroup group, int checkedId) {
  
      //Implementing tab selected listener over tablayout
      tabLayout2.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
          @Override
          public void onTabSelected(TabLayout.Tab tab) {
              viewPager2.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
              switch (tab.getPosition()) {
                  case 0:
                      Log.e(&quot;TAG&quot;,&quot;TAB1&quot;);
                      break;
                  case 1:
                      Log.e(&quot;TAG&quot;,&quot;TAB2&quot;);
                      break;
              }
          }
  
          @Override
          public void onTabUnselected(TabLayout.Tab tab) {
          }
  
          @Override
          public void onTabReselected(TabLayout.Tab tab) {
          }
      });
      }  
  
  //Setting View Pager
  private void setupViewPager2(ViewPager viewPager) {
      expAttList = new ArrayList&lt;COAAccount&gt;();
      adapter2 = new ViewPagerAdapter(getSupportFragmentManager());
      adapter2.addFrag(new AttachmentFragment(&quot;Attachments&quot;,expAttList,fontFamily), &quot;Attachments&quot;);
      adapter2.addFrag(new EventFragment(&quot;Events&quot;,fontFamily), &quot;Events&quot;);
      viewPager2.setAdapter(adapter2);
  }
  
  
  //View Pager fragments setting adapter class
  class ViewPagerAdapter extends FragmentPagerAdapter {
      private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();//fragment arraylist
      private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();//title arraylist
  
      public ViewPagerAdapter(FragmentManager manager) {
          super(manager);
      }
  
      @Override
      public Fragment getItem(int position) {
          return mFragmentList.get(position);
      }
  
      @Override
      public int getCount() {
          return mFragmentList.size();
      }
  
  
      //adding fragments and title method
      public void addFrag(Fragment fragment, String title) {
          mFragmentList.add(fragment);
          mFragmentTitleList.add(title);
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return mFragmentTitleList.get(position);
      }
  }
  </code></pre>
  <blockquote>
  <p><code>layout</code> file</p>
  </blockquote>
  <pre><code>                &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
   &lt;android.support.v4.widget.DrawerLayout
      android:id=&quot;@+id/drawer_layout&quot;
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:fab=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:local=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:fitsSystemWindows=&quot;true&quot;&gt;
   &lt;android.support.design.widget.CoordinatorLayout
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot; &gt;
  
      &lt;android.support.design.widget.AppBarLayout
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;130dp&quot;
              android:theme=&quot;@style/ThemeOverlay.AppCompat.Dark.ActionBar&quot;&gt;
  
          &lt;android.support.design.widget.CollapsingToolbarLayout
                  android:id=&quot;@+id/collapsing_toolbar&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;match_parent&quot;
                  app:contentScrim=&quot;?attr/colorPrimary&quot;
                  app:layout_scrollFlags=&quot;scroll|exitUntilCollapsed&quot;&gt;
              &lt;android.support.v7.widget.Toolbar
                      android:id=&quot;@+id/toolbar&quot;
                      android:layout_width=&quot;match_parent&quot;
                      android:layout_height=&quot;?attr/actionBarSize&quot;
                      app:layout_collapseMode=&quot;pin&quot;/&gt;
  
          &lt;/android.support.design.widget.CollapsingToolbarLayout&gt;
  
      &lt;/android.support.design.widget.AppBarLayout&gt;
  
      &lt;android.support.v4.widget.NestedScrollView
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;match_parent&quot;
              app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;&gt;
  
          &lt;LinearLayout
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:orientation=&quot;vertical&quot;
                  android:paddingTop=&quot;24dp&quot;&gt;
  
  
              &lt;android.support.v7.widget.CardView
                      android:layout_width=&quot;match_parent&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:background=&quot;@color/Lavender&quot;
                      android:layout_marginBottom=&quot;16dp&quot;
                      android:layout_marginLeft=&quot;16dp&quot;
                      android:layout_marginRight=&quot;16dp&quot;&gt;
  
                  &lt;LinearLayout
                          android:layout_width=&quot;match_parent&quot;
                          android:layout_height=&quot;wrap_content&quot;
                          android:orientation=&quot;vertical&quot;
                          android:padding=&quot;16dp&quot;&gt;
  
  
                      &lt;android.support.design.widget.TextInputLayout
  
                              android:layout_width=&quot;match_parent&quot;
                              android:layout_height=&quot;wrap_content&quot;&gt;
  
                          &lt;EditText
                                  android:id=&quot;@+id/edtName&quot;
                                  android:layout_width=&quot;match_parent&quot;
                                  android:layout_height=&quot;wrap_content&quot;
                                  android:inputType=&quot;text&quot;
                                  android:hint=&quot;Name&quot; /&gt;
  
                      &lt;/android.support.design.widget.TextInputLayout&gt;
  
                      &lt;RadioGroup
                              android:id=&quot;@+id/rdbGroup&quot;
                              android:layout_width=&quot;match_parent&quot;
                              android:layout_height=&quot;25dp&quot;
                              android:layout_marginTop=&quot;1dp&quot;
                              android:layout_marginBottom=&quot;1dp&quot;
                              android:background=&quot;@drawable/round_border&quot;
                              android:orientation=&quot;horizontal&quot; &gt;
  
                          &lt;RadioButton
                                  android:id=&quot;@+id/rdb1&quot;
                                  android:layout_width=&quot;wrap_content&quot;
                                  android:layout_height=&quot;23dp&quot;
                                  android:layout_weight=&quot;1&quot;
                                  android:background=&quot;@drawable/bg_blue&quot;
                                  android:button=&quot;@android:color/transparent&quot;
                                  android:textColor=&quot;@drawable/txt_color&quot;
                                  android:gravity=&quot;center&quot;
                                  android:paddingBottom=&quot;2dp&quot;
                                  android:paddingTop=&quot;2dp&quot;
                                  android:singleLine=&quot;true&quot;
                                  android:text=&quot;radio1&quot;
                                  android:checked=&quot;true&quot;
                                  android:textSize=&quot;13sp&quot; /&gt;
  
                          &lt;View
                                  android:id=&quot;@+id/vSep2&quot;
                                  android:layout_width=&quot;1dp&quot;
                                  android:layout_height=&quot;match_parent&quot;
                                  android:background=&quot;#000000&quot;
                                  android:visibility=&quot;visible&quot; /&gt;
  
                          &lt;RadioButton
                                  android:id=&quot;@+id/rdb2&quot;
                                  android:layout_width=&quot;wrap_content&quot;
                                  android:layout_height=&quot;23dp&quot;
                                  android:layout_weight=&quot;1&quot;
                                  android:background=&quot;@drawable/bg_red&quot;
                                  android:button=&quot;@android:color/transparent&quot;
                                  android:textColor=&quot;@drawable/txt_color&quot;
                                  android:gravity=&quot;center&quot;
                                  android:paddingBottom=&quot;2dp&quot;
                                  android:paddingTop=&quot;2dp&quot;
                                  android:singleLine=&quot;true&quot;
                                  android:text=&quot;radio2&quot;
                                  android:textSize=&quot;13sp&quot; /&gt;
                      &lt;/RadioGroup&gt;
                  &lt;/LinearLayout&gt;
              &lt;/android.support.v7.widget.CardView&gt;
  
              &lt;android.support.v7.widget.CardView
                      android:layout_width=&quot;match_parent&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:background=&quot;@color/SkyBlue&quot;
                      android:layout_marginBottom=&quot;16dp&quot;
                      android:layout_marginLeft=&quot;16dp&quot;
                      android:layout_marginRight=&quot;16dp&quot;&gt;
                  &lt;LinearLayout
                          android:layout_width=&quot;match_parent&quot;
                          android:layout_height=&quot;match_parent&quot;
                          android:orientation=&quot;vertical&quot;&gt;
  
                      &lt;android.support.design.widget.TabLayout
                              android:id=&quot;@+id/tab_layout2&quot;
                              android:layout_width=&quot;match_parent&quot;
                              android:layout_height=&quot;wrap_content&quot;
                      android:background=&quot;?attr/colorPrimary&quot;
                       android:theme=&quot;@style/ThemeOverlay.AppCompat.Dark.ActionBar&quot;
                      app:tabIndicatorColor=&quot;@android:color/holo_red_dark&quot;
                      app:tabIndicatorHeight=&quot;4dp&quot;
                              local:tabMode=&quot;scrollable&quot; /&gt;
                      &lt;android.support.v4.view.ViewPager
                              android:id=&quot;@+id/viewPager2&quot;
                              android:layout_width=&quot;match_parent&quot;
                              android:layout_height=&quot;200dp&quot;
                              android:layout_below=&quot;@id/tab_layout&quot;/&gt;
  
                  &lt;/LinearLayout&gt;
              &lt;/android.support.v7.widget.CardView&gt;
          &lt;/LinearLayout&gt;
  &lt;/android.support.v4.widget.NestedScrollView&gt;
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  
  
  &lt;android.support.design.widget.NavigationView
          android:id=&quot;@+id/navigation_view&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;match_parent&quot;
          android:layout_gravity=&quot;right&quot;
          app:headerLayout=&quot;@layout/drawer_header_expense&quot;
          app:menu=&quot;@menu/drawer&quot;/&gt;
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>