Tab not taking full width on Tablet device [Using android.support.design.widget.TabLayout]
A "simpler" answer borrowed from Kaizie would just be adding app:tabMaxWidth="0dp"
in your TabLayout
xml:
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed" />
How to make TabLayout take up full width of the screen?
Try below snippet
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed" />
tab icons not filling full width
Usefixed
tabMode instead of scrollable
app:tabMode="fixed"
Android one tab TabLayout with full width not working at first
I faced exactly the same issue and managed to solve it by setting the tabMaxWidth attribute to a high value rather (e.g. 500dp) than to 0dp as suggested in some answers.
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMaxWidth="500dp" />
Tabs in TabLayout not filling up entire ActionBar
You can refer to the TabLayout.
GRAVITY_CENTER
Gravity used to lay out the tabs in the center of the TabLayout.
GRAVITY_FILL
Gravity used to fill the TabLayout as much as possible.
MODE_FIXED
Fixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs.
MODE_SCROLLABLE
Scrollable tabs display a subset of tabs at any given moment, and can contain longer tab labels and a larger number of tabs.
Set this in your code or your layout xml.
app:tabGravity="center"
app:tabMode="fixed"
or
tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
Generally, using the code like blow can work without setting tabGravity
and tabMode
.
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
TabLayout not filling width when tabMode set to 'scrollable'
I guess this is the simpliest way to achieve what you want.
public class CustomTabLayout extends TabLayout {
public CustomTabLayout(Context context) {
super(context);
}
public CustomTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
try {
if (getTabCount() == 0)
return;
Field field = TabLayout.class.getDeclaredField("mTabMinWidth");
field.setAccessible(true);
field.set(this, (int) (getMeasuredWidth() / (float) getTabCount()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Tablayout height not working in tablet
The problem was with the parent of the tablayout. The tablayout was ok. Here is the code for parent item.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/default_app_white"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<android.support.design.widget.TabLayout
android:id="@+id/tablayout_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMaxWidth="0dp"
android:minHeight="0dp"
android:layout_gravity="bottom"
android:layout_below="@+id/relativeLayout"
app:tabGravity="fill"
app:tabBackground="@drawable/tab_color_selector"
app:tabIndicatorColor="@color/sea_green"
app:tabIndicatorHeight="2dp"
app:tabMode="fixed"
android:clickable="false"/>
</RelativeLayout>
Related Topics
Android: Failed to Allocate Memory
How to Save Image in Shared Preference in Android | Shared Preference Issue in Android with Image
Using the Limit Statement in a SQLite Query
Android Webview Click Open Within Webview Not a Default Browser
Bring Application to Front After User Clicks on Home Button
How to Set Alarm in Android Programmatically
Suppress/Block Broadcastreceiver in Another App
Android: Fragments Overlapping Issue
Downloading/Caching Google Maps for Offline Use
Image Share Intent Works for Gmail But Crashes Fb and Twitter
How to Set Text to View from Drawer Header Layout in Navigation Drawer Without Inflating View
Error:Execution Failed for Task ':App:Transformresourceswithmergejavaresfordebug'
Item with App:Showasaction Not Showing
Turning on Screen from Receiver/Service
Glass Voice Command Nearest Match from Given List
Android Emulator Camera Custom Image