How to set the divider between Tabs in TabLayout of design support library?
There is a way to add divider by using Tab
setCustomView method:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
RelativeLayout relativeLayout = (RelativeLayout)
LayoutInflater.from(this).inflate(R.layout.tab_layout, tabLayout, false);
TextView tabTextView = (TextView) relativeLayout.findViewById(R.id.tab_title);
tabTextView.setText(tab.getText());
tab.setCustomView(relativeLayout);
tab.select();
}
Tab custom layout with divider (tab_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Tab title -->
<TextView
android:id="@+id/tab_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@drawable/tab_item_selector"/>
<!-- Tab divider -->
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@android:color/black" />
</RelativeLayout>
Set TabLayout tab horizontal padding to 0dp
:
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_tabbar_background"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="4dp"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp" />
And a selector for tab title text color when it's selected (tab_item_selector.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/abc_primary_text_material_dark" />
<item android:state_focused="true" android:color="@color/abc_primary_text_material_dark" />
<item android:state_pressed="true" android:color="@color/abc_primary_text_material_dark" />
<item android:color="@color/abc_secondary_text_material_dark" />
</selector>
Add separator in tablayout in android
Firstly create custom XML file for separator:
tab_layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Assign Tab title in below text view-->
<TextView
android:id="@+id/tab_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@drawable/tab_item_selector"/>
<!-- Create separator -->
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@android:color/black" />
</RelativeLayout>
Now in your java file code as below:
for (int i = 0; i < bottomTab.getTabCount(); i++) {
TabLayout.Tab tab = bottomTab.getTabAt(i);
RelativeLayout relativeLayout = (RelativeLayout)
LayoutInflater.from(this).inflate(R.layout.tab_layout, bottomTab, false);
TextView tabTextView = (TextView) relativeLayout.findViewById(R.id.tab_title);
tabTextView.setText(tab.getText());
tab.setCustomView(relativeLayout);
tab.select();
}
and add below lines in tablayout tag:
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
tab_item_seletor.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/abc_primary_text_material_dark" />
<item android:state_focused="true" android:color="@color/abc_primary_text_material_dark" />
<item android:state_pressed="true" android:color="@color/abc_primary_text_material_dark" />
<item android:color="@color/abc_secondary_text_material_dark" />
</selector>
Is there a way to add vertical line between each tab in TabLayout
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);
TabLayout set spacing or margin each tab
Been fighting this problem for a while too, found the solution on this thread : Android Design Support Library TabLayout using custom tabs layout but layout wrapping the tabs
<!-- Add the padding to tabPaddingStart and/or tabPaddingEnd -->
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/tab_layout_height"
app:tabPaddingStart="10dp"
app:tabPaddingEnd="10dp">
Android Design Support Library: TabLayout tabs text in single line
Just add app:tabMode="scrollable"
to TabLayout;
<android.support.design.widget.TabLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
app:tabMode="scrollable"
android:layout_height="wrap_content" />
Related Topics
Will Google Android Ever Support .Net
How to Get a Dialog Style Activity Window to Fill the Screen
Gradle Build Fails on Lint Task
Android Background Image Memory Usage
Onsharedpreferencechanged Not Fired If Change Occurs in Separate Activity
How to Get the Android Path String to a File on Assets Folder
Differencebetween Activity and Context
Android Share Images from Assets Folder
Android Device Manager Fails to Launch After Updating to MACos Big Sur 11.3
How to Copy Text to Clip Board in Android
How to Have a Transparent Imagebutton: Android
Navigation Drawer Semi-Transparent Over Status Bar Not Working
Remove Image from Cache in Glide Library
Lazy Load Images on Listview in Android(Beginner Level)
How to Set Android_Sdk_Home Environment Variable
Notificationcompat with API 26
Adb Is Not Recognized as Internal or External Command on Windows