The TabLayout is quite userful building a sliding tab for viewpager, except there is no way you can add vertical line between tabs just like TabHost in code or xml as far as I know, so is there other way to do so with ease?
Asked
Active
Viewed 1.2k times
2 Answers
55
TabLayout is actually a horizontal scrollable LinearLayout.
Just use following code to add dividers:
LinearLayout linearLayout = (LinearLayout)tabLayout.getChildAt(0);
linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(Color.GRAY);
drawable.setSize(1, 1);
linearLayout.setDividerPadding(10);
linearLayout.setDividerDrawable(drawable);

kim
- 1,057
- 11
- 11
-
3
-
2
-
This solution works for me, but its also add a divider before the first item even i set show_divider_middle. – Mustafa Sadedil Mar 04 '17 at 13:41
-
-
This isn't true for the newer TabLayout in material package. See: `com.google.android.material.tabs.TabLayout` which is a `HorizontalScrollView` – Christopher Perry Mar 17 '21 at 23:28
0
try this,
You can add manually line in your tab_indicator layout file.
for Horizontal line,
<View
android:layout_height="1dp"
android:id="@+id/line"
android:layout_width="fill_parent"
android:background="your color" />
and for Vertical line
<View
android:layout_height="7dp"
android:id="@+id/line"
android:layout_width="1dp"
android:background="your color" />

Ganpat Kaliya
- 888
- 2
- 9
- 16