title: TabLayout : How to add swipe AND onClick to switch tabs
body: <p>My tablayout is working perfectly, tabs are switching right/left and displaying content correctly. </p>
  
  <p>But when "Clicking" on the tabs, no action happens. </p>
  
  <p>Question : How can I do to switch tabs with a onClick ? I tried to add an onClickListener on each tab but it's not working. </p>
  
  <p>Here is my XML code: </p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout
      android:id="@+id/main_layout"
      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=".MainActivity"&gt;
  
      &lt;android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:background="?attr/colorPrimary"
          android:elevation="6dp"
          android:minHeight="?attr/actionBarSize"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
          app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/&gt;
  
      &lt;android.support.design.widget.TabLayout
          android:id="@+id/tab_layout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/toolbar"
          android:background="?attr/colorPrimary"
          android:elevation="6dp"
          android:minHeight="?attr/actionBarSize"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/&gt;
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/pager"
          android:layout_width="match_parent"
          android:layout_height="fill_parent"
          android:layout_below="@id/tab_layout"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Here is my Java</p>
  
  <pre><code>package com.example.alexis.tablayout;
  import android.os.Bundle;
  import android.support.design.widget.TabLayout;
  import android.support.v4.view.ViewPager;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  
  public class MainActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      setTitle(getResources().getText(R.string.MOD));
  
      final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
      final TabLayout.Tab Manof = tabLayout.newTab();
      final TabLayout.Tab Catalogue = tabLayout.newTab();
      final TabLayout.Tab Lidory = tabLayout.newTab();
  
      Manof.setIcon(R.drawable.flamepink);
      Catalogue.setIcon(R.drawable.modwhite);
      Lidory.setIcon(R.drawable.pantywhite);
  
      tabLayout.addTab(Manof, 0);
      tabLayout.addTab(Catalogue, 1);
      tabLayout.addTab(Lidory, 2);
      tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
  
      final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
      final PagerAdapter adapter = new PagerAdapter
              (getSupportFragmentManager(), tabLayout.getTabCount());
      viewPager.setAdapter(adapter);
  
      viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
      viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  
          @Override
          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  
          }
  
          @Override
          public void onPageSelected(int position) {
  
              switch (position){
                  case 0:
                      Manof.setIcon(R.drawable.flamepink);
                      Catalogue.setIcon(R.drawable.modwhite);
                      Lidory.setIcon(R.drawable.pantywhite);
                      setTitle("Man Of The Day");
                      break;
  
                  case 1:
                      Manof.setIcon(R.drawable.flamewhite);
                      Catalogue.setIcon(R.drawable.modpink);
                      Lidory.setIcon(R.drawable.pantywhite);
                      setTitle("Catalogue");
                      break;
                  case 2:
                      Manof.setIcon(R.drawable.flamewhite);
                      Catalogue.setIcon(R.drawable.modwhite);
                      Lidory.setIcon(R.drawable.pantypink);
                      setTitle("Lidory");
                      break;
              }
          }
  
          @Override
          public void onPageScrollStateChanged(int state) {
          }
      });
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();
      if (id == R.id.Menu) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }}
  </code></pre>