Source:
private void setupTabs() { if (shouldShowTabs()) { int normalColor = ContextCompat.getColor(this, R.color.blue_light); int selectedColor = ContextCompat.getColor(this, R.color.white); mTabLayout.setTabTextColors(normalColor, selectedColor); mTabLayout.addTab(mTabLayout.newTab().setText(R.string.media_all)); mTabLayout.addTab(mTabLayout.newTab().setText(R.string.media_images)); mTabLayout.addTab(mTabLayout.newTab().setText(R.string.media_documents)); mTabLayout.addTab(mTabLayout.newTab().setText(R.string.media_videos)); mTabLayout.addTab(mTabLayout.newTab().setText(R.string.media_audio)); mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { setFilter(getFilterForPosition(tab.getPosition())); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { setFilter(getFilterForPosition(tab.getPosition())); } }); mTabLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { mTabLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this); if (mTabLayout.getChildCount() > 0) { int tabLayoutWidth = 0; LinearLayout tabFirstChild = (LinearLayout) mTabLayout.getChildAt(0); for (int i = 0; i < mTabLayout.getTabCount(); i++) { LinearLayout tabView = (LinearLayout) (tabFirstChild.getChildAt(i)); tabLayoutWidth += (tabView.getMeasuredWidth() + ViewCompat.getPaddingStart(tabView) + ViewCompat.getPaddingEnd(tabView)); } int displayWidth = DisplayUtils.getDisplayPixelWidth(MediaBrowserActivity.this); if (tabLayoutWidth < displayWidth) { mTabLayout.setTabMode(TabLayout.MODE_FIXED); mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL); } } } }); } else { mTabLayout.setVisibility(View.GONE); } }

Target:
private void setupTabs() { if (shouldShowTabs()) { int normalColor = ContextCompat.getColor(this, R.color.blue_light); int selectedColor = ContextCompat.getColor(this, R.color.white); mTabLayout.setTabTextColors(normalColor, selectedColor); mTabLayout.removeAllTabs(); mTabLayout.addTab(mTabLayout.newTab().setText(R.string.media_all)); mTabLayout.addTab(mTabLayout.newTab().setText(R.string.media_images)); mTabLayout.addTab(mTabLayout.newTab().setText(R.string.media_documents)); mTabLayout.addTab(mTabLayout.newTab().setText(R.string.media_videos)); mTabLayout.addTab(mTabLayout.newTab().setText(R.string.media_audio)); mTabLayout.clearOnTabSelectedListeners(); mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { setFilter(getFilterForPosition(tab.getPosition())); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { setFilter(getFilterForPosition(tab.getPosition())); } }); mTabLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { mTabLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this); if (mTabLayout.getChildCount() > 0) { int tabLayoutWidth = 0; LinearLayout tabFirstChild = (LinearLayout) mTabLayout.getChildAt(0); for (int i = 0; i < mTabLayout.getTabCount(); i++) { LinearLayout tabView = (LinearLayout) (tabFirstChild.getChildAt(i)); tabLayoutWidth += (tabView.getMeasuredWidth() + ViewCompat.getPaddingStart(tabView) + ViewCompat.getPaddingEnd(tabView)); } int displayWidth = DisplayUtils.getDisplayPixelWidth(MediaBrowserActivity.this); if (tabLayoutWidth < displayWidth) { mTabLayout.setTabMode(TabLayout.MODE_FIXED); mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL); } } } }); } else { mTabLayout.setVisibility(View.GONE); } }