Last active
April 27, 2018 09:28
-
-
Save Sottti/7828dd74f234c28fdffd8b6608c8b291 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.design.widget.TabLayout; | |
import android.util.AttributeSet; | |
import android.widget.LinearLayout; | |
public class AdaptiveTabLayout extends TabLayout | |
{ | |
private boolean mGravityAndModeSeUpNeeded = true; | |
public AdaptiveTabLayout(@NonNull final Context context) | |
{ | |
this(context, null); | |
} | |
public AdaptiveTabLayout(@NonNull final Context context, @Nullable final AttributeSet attrs) | |
{ | |
this(context, attrs, 0); | |
} | |
public AdaptiveTabLayout | |
( | |
@NonNull final Context context, | |
@Nullable final AttributeSet attrs, | |
final int defStyleAttr | |
) | |
{ | |
super(context, attrs, defStyleAttr); | |
setTabMode(MODE_SCROLLABLE); | |
} | |
@Override | |
protected void onLayout(final boolean changed, final int l, final int t, final int r, final int b) | |
{ | |
super.onLayout(changed, l, t, r, b); | |
if (mGravityAndModeSeUpNeeded) | |
{ | |
setModeAndGravity(); | |
} | |
} | |
private void setModeAndGravity() | |
{ | |
final int tabCount = getTabCount(); | |
final int screenWidth = UtilsDevice.getScreenWidth(); | |
final int minWidthNeedForMixedMode = getMinSpaceNeededForFixedMode(tabCount); | |
if (minWidthNeedForMixedMode == 0) | |
{ | |
return; | |
} | |
else if (minWidthNeedForMixedMode < screenWidth) | |
{ | |
setTabMode(MODE_FIXED); | |
setTabGravity(UtilsDevice.isBigTablet() ? GRAVITY_CENTER : GRAVITY_FILL) ; | |
} | |
else | |
{ | |
setTabMode(TabLayout.MODE_SCROLLABLE); | |
} | |
setLayoutParams(new LinearLayout.LayoutParams | |
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); | |
mGravityAndModeSeUpNeeded = false; | |
} | |
private int getMinSpaceNeededForFixedMode(final int tabCount) | |
{ | |
final LinearLayout linearLayout = (LinearLayout) getChildAt(0); | |
int widestTab = 0; | |
int currentWidth; | |
for (int i = 0; i < tabCount; i++) | |
{ | |
currentWidth = linearLayout.getChildAt(i).getWidth(); | |
if (currentWidth == 0) return 0; | |
if (currentWidth > widestTab) | |
{ | |
widestTab = currentWidth; | |
} | |
} | |
return widestTab * tabCount; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout | |
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"> | |
<com.stackoverflow.example.AdaptiveTabLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="?colorPrimary" | |
app:tabIndicatorColor="@color/white" | |
app:tabSelectedTextColor="@color/text_white_primary" | |
app:tabTextColor="@color/text_white_secondary" | |
tools:layout_width="match_parent"/> | |
</FrameLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.res.Resources; | |
public class UtilsDevice extends Utils | |
{ | |
private static final int sWIDTH_FOR_BIG_TABLET_IN_DP = 720; | |
private UtilsDevice() {} | |
public static int pixelToDp(final int pixels) | |
{ | |
return (int) (pixels / Resources.getSystem().getDisplayMetrics().density); | |
} | |
public static int getScreenWidth() | |
{ | |
return Resources | |
.getSystem() | |
.getDisplayMetrics() | |
.widthPixels; | |
} | |
public static boolean isBigTablet() | |
{ | |
return pixelToDp(getScreenWidth()) >= sWIDTH_FOR_BIG_TABLET_IN_DP; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment